Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Jest tests use more and more memory until the run slows down or dies

ErrorJest tests are leaking memory

Short 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.

  • Applies to Jest 30, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 --runInBand
  2. 2Match what you see, then jump to its fix

    1. 1Heap values rise across many files and the run slows toward the endWorkers keep memory between test filesFix
    2. 2Memory spikes at the start of a run, on laptops, containers or CI with little RAMToo many parallel workers for the machineFix

What does the error look like?

  • Jest tests are leaking memory
  • Memory 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 40

2Fix

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 jest

Confirmed by jestjs.io

Did this fix it?

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 -h

2Fix

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 --runInBand

3Verify

Peak memory during the run is lower and the process is no longer killed.

npx jest --maxWorkers=2 --logHeapUsage

Confirmed by jestjs.io

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.