Issue
After upgrading NodeJS to v20 my project started to run into the following error when running its tests:
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
As well as taking over 2h to run its tests... (from 15 mins previously)
The project has a few thousand tests built for most of the angular components used. The tests are executed with Jest.
The problem does not happen when running the same tests with Nodejs v16... (we want them to run with node v20)
running the tests with 6 threads in parallel and with the --logHeapUsage
flag demostrates that there is a sizeable memory leack going on with the new node version (heap size is innially 310mb, and keeps groing until 4GB, and beyond):
npx jest --logHeapUsage
(... a lot of other ones)
plucking the error outy of the pic:
<--- Last few GCs --->
[11248:000002CB7489B580] 762926 ms: Mark-Compact (reduce) 4031.9 (4143.6) -> 4031.7 (4143.6) MB, 5514.89 / 0.00 ms (average mu = 0.169, current mu = 0.018) allocation failure; scavenge might not succeed
[11248:000002CB7489B580] 768007 ms: Mark-Compact (reduce) 4032.7 (4143.6) -> 4032.6 (4144.6) MB, 5078.96 / 0.00 ms (average mu = 0.090, current mu = 0.001) allocation failure; scavenge might not succeed
<--- JS stacktrace --->
Test suite failed to run
A jest worker process (pid=11248) was terminated by another process: signal=SIGTERM, exitCode=null. Operating system logs may contain more information on why this occurred.
at ChildProcessWorker._onExit (../../../node_modules/jest-worker/build/workers/ChildProcessWorker.js:370:23)
The threads die as they breach 4GB of memory. The tests seems to run slower as the memory heap gets bigger.
My CI server and local computers can handle 100GB of memory consumption, so I added the parameter max-old-space-size
as suggested here. The tests would no longer run out of memory, but the issue is that they start to take more than 2 hours to run in node v20 while they were taking 15 mins in node v16. So allowing the memory to leak (by setting a bigger max-old-space-size) is not an acceptable outcome...
I am currently trying to call node's garbage collection as the test execution progresses. I could not find a way to do it...
How should I proceed to call node's Garbage collection?
Or even better, would you have another suggestion to help mitigate the general leak?
Solution
It turns out it was a bug on NodeJs...
I found this issue thread where people were discussing memory leaking when running jest tests with nodev20
As part of node v20.10 (released last week) fixes were introduced for this (details in the above thread link).
I can confirm that once I installed node v20.10 and re-ran the tests, the memory leak stopped and the tests are back to running within the expected time frame!
Answered By - The Fabio
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.