[Bug]: Memory consumption issues on Node JS 16.11.0+
See original GitHub issue🚨 Using Jest 29, you can use --workerIdleMemoryLimit
. See https://jestjs.io/docs/configuration/#workeridlememorylimit-numberstring 🚨
Version
27.0.6
Steps to reproduce
- Install the latest Node JS (16.11.0 or later) or use the appropriate Docker image
- Set up a project with a multiplicity Jest tests
- Run
node --expose-gc node_modules/.bin/jest --logHeapUsage
and see how the memory consumption starts increasing.
Expected behavior
Since Jest calls global.gc()
when Garbage Collector is exposed and --logHeapUsage
flag is present, the memory usage should be stable.
Actual behavior
The memory usage increases with every new test
Additional context
We had some issues with Jest workers consuming all available RAM both on CI machine and locally.
After doing some research, we found that if we run Jest like the following node --expose-gc node_modules/.bin/jest --logHeapUsage
, the heap size remains stable. After upgrading to Node JS v16.11.0, the issue was back. Node v16.10.0 works fine. I believe it was something accidentally introduced in the new Node, but it might be useful to take a look at this from Jest perspective in search of possible workarounds.
I’m also having the same behavior on my working machine, environment of which I’m pasting below 👇🏻
Environment
System:
OS: macOS 11.6
CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
Binaries:
Node: 16.11.0 - ~/.nvm/versions/node/v16.11.0/bin/node
Yarn: 1.22.0 - ~/SomeFancyDir/webapp/node_modules/.bin/yarn
npm: 8.0.0 - ~/.nvm/versions/node/v16.11.0/bin/npm
npmPackages:
jest: 27.0.6 => 27.0.6
Issue Analytics
- State:
- Created 2 years ago
- Reactions:134
- Comments:157 (47 by maintainers)
Top GitHub Comments
We’ve been recently hit by this bug (or combination of them) in several services of our stack. It’s been an inconvenience for use since once the heap size exceeded the 2GB available by GitHub Actions machines, our CI/CD process was failing 100% of the time.
I believe the “modules accumulating in the heap” bug has been present in Jest for some time, but that in combination with the changes in Node from
v16.10
tov16.11
has made it much more severe. We’re certain that bug exists in Jest, as we’ve managed to replicate it with a “dummy repo”, as several other people have stated.The fact that the ticket created in Node JS was closed as a WONTFIX is a bit worrying. I currently lack the knowledge to judge the discussions which happened there, but if that wasn’t a bug at the end probably the solution falls on the Jest side.
After trying to extract as much information from the different GitHub issues that have been created in Jest, TS-Jest and Node, and trying several approaches, the only path for us was to downgrade back to Node
v16.10
.These are some of the statistics we gathered in the process:
@jest/core
Some explanations about the columns:
@jest/core
: version of the Jest corejest.config.json
--runInBand
Jest flag--no-compilation-cache
flagThe most relevant tickets related to this matter:
We hope this issue is given the importance it deserves, as keeping the Node version pinned to
v16.10
or relying on bigger CI machines to compensate this is a poor strategy for the future.Our company created a custom runtime based on the patch above that is located here: https://github.com/reside-eng/jest-runtime
This runtime will only work with Jest v28 and it will not work if you are using ESM modules. There is some additional information in the README.md about using swc if you are also using Typescript, but this is not required to use the custom runtime.
then add the following to your jest.config.js