This repository demonstrates an automated approach to detect memory leaks in Docker containers using a GitHub Actions workflow. It leverages the scripts/checkForMemoryLeaks.ts
script that checks for memory leaks in your code, which is run as part of an end-to-end (E2E) testing pipeline using GitHub Actions.
Detecting memory leaks in software applications is a significant challenge in software development. Memory leaks can degrade the performance of an application over time and can lead to system crashes. To tackle this issue, this repository provides a GitHub Actions workflow that utilizes Docker containers and a custom Node.js script for automated memory leak detection during the E2E testing process.
-
scripts/checkForMemoryLeaks.ts
Script: This Node.js script checks the memory usage of a Docker container before and after the E2E tests. If the final memory usage exceeds a certain threshold (50% by default), the script flags a potential memory leak and halts the process. The script takes three command-line arguments: the Docker container's name, its initial memory usage, and its final memory usage. -
.github/workflows/e2e-tests.yml
Workflow: This GitHub Actions workflow orchestrates the process. It records the initial memory usage of the Docker containers, runs the E2E tests, and then records the final memory usage. These values are then fed to thescripts/checkForMemoryLeaks.ts
script for analysis.
The workflow also prepares and uploads the logs of all containers as an artifact. This facilitates debugging and helps in identifying issues during the E2E tests.
To use this workflow in your project, follow these steps:
- Install Docker and GitHub CLI on your local machine.
- Clone this repository:
gh repo clone <repository-url>
. - Navigate into the repository's directory:
cd <repository-name>
. - Adjust the script and workflow files according to your project's specific needs.
- Push the changes to your repository.
Remember to replace the container names in the CONTAINERS_TO_MEM_CHECK
and CONTAINERS_TO_LOG
environment variables in the .github/workflows/e2e-tests.yml
file with the names of your own Docker containers.
Although this solution provides an automated approach to detect memory leaks, it does have certain limitations:
- Possible False Alarms: The script might produce false positives (flagging normal memory usage increases as leaks) or false negatives (missing actual memory leaks).
- Memory Threshold Balance: The threshold for memory usage increase should be carefully chosen. A high threshold could miss minor leaks, whereas a low threshold might lead to frequent false alarms.
- Variations in Memory Management: Memory management in Docker containers can vary depending on the technology stack of your application, which might affect the accuracy of this solution.
For a detailed guide on how this setup works and how to integrate it into your own projects, check my blog post Automating Memory Leak Detection: Improve Your Dockerized CI Pipelines Now. The post explains each step of the process and provides additional context and explanation.