-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Jest leaks memory in ESM mode #14605
Comments
Here is a log:
|
tested with nodejs v21.0.0-nightly202310041a839f388e (06-Oct-2023 11:00) and it is still an issue. |
You mentioned JSON files in the other PR. This is how we construct them in ESM mode: jest/packages/jest-runtime/src/index.ts Lines 492 to 500 in 00ef0ed
Essentially copied straight from the docs (which use a JSON example): https://nodejs.org/api/vm.html#class-vmsyntheticmodule So there's no code cache here. I'd assume But yeah, maybe the flags suggested in #12205 (comment) can help in narrowing down what's not freed? |
@Havunen this does not reproduce if I remove the |
I commented out all importModuleDynamically callbacks it still does not work using node v21.0.0-nightly20231008fce8fbadcd. |
log:
All |
I also removed all |
I have reproduced the issue using nodejs and javascript only, and also found a work around. Having a shared context between the modules seems to leak the memory. However setting the shared context null manually after function execution it fixes the memory leak. Setting the variable null in JS is non-sense (ref: https://github.com/Havunen/nodejs-memory-leak/blob/main/test.js#L45-L46 ) because it goes out of scope and should be GC'd but it does not seem to happen. So its definetly a nodejs / v8 bug https://github.com/Havunen/nodejs-memory-leak |
We do set it to
Not so in the JSDOM env, but the reproduction in this issue uses the Node env. Might be we keep a reference without meaning to within the runtime of course |
yeah but it is not set null in the call site not sure if it needs to be nulled everywhere or how it works in low level |
After testing a bit more it seems setting the context to null does not really fix the issue :/ |
Edit: same issue with or without cross-env. 😢 Just curious, but does removing Noticed it's in maintenance only mode so I'm curious if nodejs 20 is supported. https://www.npmjs.com/package/cross-env. 4 years ago was last publish...so it would have only supported maybe up to Node12 |
Workaround for this is to do the following. Whatever your You can pass it around as a process.env variable too...or read the docs further there might be a spot to access this heapsize within the nodejs process (see my bullet).
jest.config.js that works with your repo diff --git a/jest.config.js b/jest.config.js
index 99cbe34..6f28936 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -10,6 +10,7 @@ export default {
transformIgnorePatterns: [
'/node_modules/',
],
+ workerIdleMemoryLimit: '128MB',
collectCoverage: false,
coverageReporters: [],
moduleDirectories: ['node_modules'] |
Regarding CC: @SimenB thoughts? |
Version
29.7.0
Steps to reproduce
clone the repository at: https://github.com/Havunen/jest-memory-leak
run
npm install
run
npm test
You should see the heap memory increasing over time until the nodejs process crashes.
Expected behavior
The nodejs process should be able to clean the memory and preferably allocate as little as possible
Actual behavior
The node js process crashes
Additional context
This is continuation from here: #12205 (comment)
Environment
The text was updated successfully, but these errors were encountered: