Jest tests use more and more memory until the run slows down or dies
Jest tests are leaking memoryShort answer
Jest runs test files in worker processes, and a worker can keep memory from earlier files. Measure first with --logHeapUsage and --runInBand. Then recycle bloated workers with workerIdleMemoryLimit, or reduce parallelism with --maxWorkers when the machine is small, such as a CI container.
Fix it fast
1Find out which cause you have
Run the suite in one process and log heap size after every test. A number that climbs and never falls points at the test or module that keeps data alive.
node --expose-gc node_modules/.bin/jest --logHeapUsage --runInBand2Match what you see, then jump to its fix
What does the error look like?
Jest tests are leaking memoryMemory use grows with every test file until the run slows down or is killed
Cause 1 of 2
Workers keep memory between test files
1Check
Compare the heap column from the first step at the start and end of the run.
node --expose-gc node_modules/.bin/jest --logHeapUsage --runInBand 2>&1 | tail -n 402Fix
Tell Jest to kill and restart a worker after a test file when its memory is above a limit. Set it in the root config; it is ignored inside a project config.
// jest.config.js
module.exports = {
workerIdleMemoryLimit: '512MB',
};3Verify
Run the suite normally and watch memory stay flat while workers restart.
npx jestConfirmed by jestjs.io
Cause 2 of 2
Too many parallel workers for the machine
1Check
By default Jest starts one worker per core minus one, and every worker is a separate process with its own memory.
free -h2Fix
Cap the pool, or use a percentage on machines whose core count varies. Use --runInBand to run in a single process when the box is very small.
npx jest --maxWorkers=50%
# or
npx jest --runInBand3Verify
Peak memory during the run is lower and the process is no longer killed.
npx jest --maxWorkers=2 --logHeapUsageConfirmed by jestjs.io
How do I stop it coming back?
- Run jest --logHeapUsage --runInBand in CI occasionally to see heap growth before it becomes a crash.
- Reset what tests create: clear timers, close servers and database handles in afterAll.
- Set maxWorkers explicitly in CI instead of relying on the detected core count.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix