jest: [Bug]: Jest increase heap usage after each test

Version

29.5.0

Steps to reproduce

  • Create a suite with many tests using react (react-testing-library)
  • Add args to jest command: --detectOpenHandles --logHeapUsage --forceExit --clearMocks
  • Show heap usage increase after each test

Expected behavior

Jest not use a lot of memory

Actual behavior

When I run tests with --logHeapUsage then starts using ~130MB of memory and after some tests the heap usage increase between 2GB and 3GB…

command:

craco test --watchAll=false --detectOpenHandles --logHeapUsage --forceExit --clearMocks

image

This test uses ~1500MB of memory image

But when I run him specifically the heap use is ~150MB

image

How to find the problem and solve it?

Additional context

Related:
https://github.com/facebook/jest/issues/11956

I tried to use workerIdleMemoryLimit but not works

Docs:
https://jestjs.io/docs/next/configuration#workeridlememorylimit-numberstring

Command:

craco test --watchAll=false --coverage --detectOpenHandles --logHeapUsage --forceExit --clearMocks --workerIdleMemoryLimit=500MB

Result:

image

Environment

System:
    OS: Linux 6.2 Pop!_OS 22.04 LTS
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.15.0/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
  npmPackages:
    jest: ^29.5.0 => 29.5.0

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 20 (1 by maintainers)

Most upvoted comments

I read this commend and I used this node option: --no-compilation-cache

https://github.com/facebook/jest/issues/11956#issuecomment-1458178986

I tried this command:

node --expose-gc --no-compilation-cache ./node_modules/.bin/craco test --watchAll=false --coverage --maxWorkers=4 --logHeapUsage

This command reduce memory consumption of ~2GB ~3GB to ~1GB (max)

But the memory leak problem persist:

image image image image

See this test:

Jest says use 1GB when running with all tests

image

But running alone, Jest says use 95MB

image

See #11956

By the looks of it, the issue is related to node rather then Jest itself. So don’t worry, you’re not the only one with this issue

I’ve switched over to @swc/jest from ts-jest and it seems like all my problems have gone away, so I think a lot of my issues may be a transpilation issue coming from ts-jest. Overall I do still think my RAM consumption still seems to be overkill, but it does seem to be doing better. I’ll have to dig into that article and see if I can track down what’s causing the consumption.

Been stuck trying to get our coverage to work with @swc/jest so I might be stuck on ts-jest for now. I did find this relevant issue it seems: https://github.com/kulshekhar/ts-jest/issues/1967

This comment https://github.com/kulshekhar/ts-jest/issues/1967 Brings up a jest custom runtime which seemed to help a bit with memory consumption, but not my overall slowness and hesitation for my tests to even start up. https://github.com/reside-eng/jest-runtime

Edit/Update: We switched over to @swc/jest and our test suite is solid and running in about 16 seconds now 🎉

Also found that the coverage shenanigans were patched in ts-jest here:

And found the following open bugs with a proposed similar fix:

I read this commend and I used this node option: --no-compilation-cache

✅ This worked for us. It was passing on our local machine but was failing in our pipelines on Azure, even after allocating more resources.

We suspect that one of the causes of our issue was the use of Inversify. During each test, we had to spin up and rebind containers. Using the --no-compilation-cache node option resolved our issue for the time being.

We are having the same issue, our ~2000 tests start to take ~18 minutes and hit OOM I took few snapshots and noticed this image

It seems to me that everything been loaded many times, not sure why?

Is this related ?