-
-
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
jsdom or babel-polyfill cause memory leak (?) #1893
Comments
Some more specific data on the heap growth in the example repo, from running it on my machine.
The heap size after the first test is 45MB and it grows linearly with a slope of 0.48.
The heap size after the first test is 48MB and it grows linearly with a slope of 1.03. Is that to be expected? This is 1MB growth on the heap for this test:
|
I've done a little research on this one, but not deep enough. Indeed we seem to leak some memory here. But it's not because of Tried to profile this with Chrome DevTools, but it didn't show timeline for Allocation Timelines and I didn't have too much time to wonder why.
Any help on this one is highly appreciated! |
@thymikee, what really confuses me is that his repo requires babel-polyfill as a dev dependency, but he doesn't actually load it anywhere in the project. How/why would this make it worse? |
When using babel-jest and when babel-polyfill is available, Jest will load it automatically. |
@cpojer, but he's not using babel-jest, just jest. |
babel-jest is used as a dep of jest-runtime |
FWIW I repeated the run of Quentin With babel: 44MB + 1.00MB * specs run |
Further investigation: the suspect is When running Jest with Below are results after running couple of hundred tests:
Since |
Do you know what exactly is being retained or what kind of stuff may lead to cycles? We may want to unset more things in the |
Update: Managed to separate the case with |
See my comment in jsdom/jsdom#1682 : the memory leak in jsdom only happens when window.close() is called synchronously after jsdom.jsdom() I see two options here:
|
I did research further. It seems that JSDOM is just a drop in an ocean. If I implement a fix for JSDOM locally, I still get huge memory leaks. However, as soon as I remove babel-polyfill, the leaks drop by 95%. IT seems that certain shims in corejs (the Object getters/setters, for example) are causing the leaks when combined with jest. A solution would be disabling babel-polyfill and starting jest with
|
95% drops are interesting, I have experienced some drops, but not this big, and the stack was still growing fast. |
I'm wondering: do we even need babel-polyfill in recent versions of Node? Shall we just not load it (except for regenerator, which we may still need). |
...unless you want to test with exactly the same setup as the "real" thing. The node and core.js implementations of the ES6/7 features could be different. ;-) Still, it's better to get rid of it, instead of having OOM problems in bigger test suites. Or, at least, giving an option to disable auto-loading of babel-polyfill. |
Okay, here are my findings so far:
If I remove babel-polyfill, the memory usage still grows during the tests, since all the windows are still in the memory. How much it grows depends on what your tests are how you utilize the windows. Right after the tests (probably on the next tick) the memory drops since all windows are destroyed. Babel-polyfill makes the whole thing even worse, since (1) its memory leak makes the memory usage grow even faster from test-to-test and (2) the windows are not destroyed, even after the tests complete. Just removing babel-polyfill alleviated the problem in one of our projects (memory almost does not grow). However we are still having issues on another, bigger one (500 tests) where the tests OOM randomly just because the windows are not destroyed right after completion of each test. How do we solve it? Well, just removing babel-polyfill alleviates the problem for smaller projects/tests. Fixing jsdom issue while having babel-polyfill on board would not fix anything, since the windows would not get destroyed because of the leakage. That means that we need to tackle both issues at once:
|
Are you sure BTW, I appreciate your help on investigating that so much, thank you! |
It's in our all interest to get this issue fixed. So you are most welcome! ^^ Check out the code in my comment on the jsdom issue: If I use the jsdom.env version for spwanWindow and print the heap usage, I barely see any memory spikes at all. Maybe my methodology is wrong. Can you try it? |
Sure I have tried that and it indeed worked for me (but didn't check in the inspector). I need to find some time to test it better in Jest, because last time it was a really quick check, where I could miss something! |
Okay. We have this issue fixed in our project with a small workaround. As already said earlier, both issues (jsdom and babel-polyfill) have to be tackled in order to have a constant memory usage.
Following example of a setup file:
Now you can run jest with:
Profit! In the jest project, following could be done to make the life easier:
|
Hi there, let me add some observations from our project. I am trying to migrate our Mocha stack to Jest and I encountered similar problems. I've tried removing Our test environment is quite complicated, we mock canvas rendering context in jsdom, we still have half of our codebase in I realized that besides transforming We have some async-await stuff in our codebase so we need Since we don't really need the whole Anyway, I think the best solution would be to solve this on |
@thymikee any idea when this will be released? Also hitting issues with memory leaks on circleci and the setImmediate change appears to do the trick. Thanks for tracking it down! |
It will land in Jest 19. We plan to release it in a week. |
This should be fixed now for the most part, right? :) |
We upgraded to Jest 19 in the hopes that this memory leak would be fixed, but unfortunately that's not the case. We've been using the As a result, Circle CI gives us this failure: Is anyone else still plagued by this memory leak in Jest 19? |
Yes -- after upgrading to Jest 19, we were also still seeing a memory leak with a combination of Removing |
FWIW, it seems the issue only presents itself when running async, as the |
@mute I did an |
I apologize. I forgot to merge #2755 into the release (honestly I thought I did), it will be part of the next release. |
All good @cpojer. I'm just glad it's fixed! |
@turadg if it takes a while, you can always do what we're doing (for now) so you don't waste any more time. Look at our {
"scripts": {
"test": "jest",
"test:ci": "suites=\"fooFolder barFolder bazFolder\"; STATUS=0; for i in $suites; do npm test -- \"${i}/*\" --runInBand; STATUS=$(( $? > $STATUS ? $? : $STATUS )); done; exit $STATUS"
}
} |
@cpojer it's been a week since the last comment and still no release. Any idea when the next one will be? |
No, there is no fixed release schedule here. There may be one next week. |
Greetings. It's April 5th and we are still experiencing this issue on Jest 19.0.2. Originally when running jest, I was up to 500s elapsed time and 1.6Gb ram usage per concurrent test when I ran out of patience and killed jest. After removing Now, if I run node with --expose-gc, suddenly the heap stops growing. Heap size per spec is around 50Mb. Now in our case, we are migrating from Karma, and our test time was around 35 seconds for 550 test specs. Unfortunately, replacing jsdom with node env is not a feasible solution currently. It appears that this issue was supposed to be fixed in v19. But is it? |
Can you try |
What's the hold up on a patch release? |
@cpojer's time off mainly. Although there's a chance we could roll out 19.1, but @DmitriiAbramov is the one to decide. |
@thymikee I'll give it a try. Update: @next (19.1.0-alpha.eed82034), with
Note: "running slow" here means:
After removing
|
@thymikee I was facing the same memory issue and then very slow tests. I made a fresh install of |
@jedmao We still had the same issue after updating to Jest 20.0.1 with CircleCI. We were able to fix it by using Testing showed that one worker was the best number (fastest tests). |
@thymikee thanks for the tip. It appears that I cannot That said, I ran the tests @ 20x slowdown with |
@mlanter I have a feeling that I think I'm fixed here, folks! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hello.
First of all thanks for this project, it's been great so far. I'm running into a issue where when Jest can resolve babel-polyfill the node memory heap keeps on growing resulting in memory allocation errors in node 5 or extremely slow test in node 6 when node starts to reach the memory limit.
This very simple repository reproduce the issue https://github.com/quentin-/jest-test
node: 6.7.0
The text was updated successfully, but these errors were encountered: