Memory Leak when running multiple test. Especially with --runInBand
See original GitHub issue🐛 Bug Report
Hi. I’m seeing a memory leak while trying to run a basic autogenerated ( with “ng g c my-test” and "ng g c “my-test-2”) Angular component. If i run jest with --detectLeak , i get messages saying that there are leaks. If i run jest with --runInBand , i see heap size going up … when i add enough of those “blank” tests or reduce the size with --max_old_space_size= , I run out of memory. From what i was able to see the teardown (between tests) is not happening properly until it finishes the very last test. Any suggestions on what to do? versions: jest: 23.6.0, jest-preset-angular: 6.0.1, jest-leak-detector: 23.6.0, zone.js: 0.8.26
To Reproduce
Steps to reproduce the behavior:
- create a bunch of Angular components with tests using “ng g c my-test-cmp-1”, “…cmp-2”, etc
- run “jest --runInBand --logHeapUsage components/” so it runs all the tests
- observe heap size grow as many times there are tests. Also, if you take a heapdump, you could see a bunch of “global > proto :: Window” entries. One for each test.
- observe that environments for each test are not torn down until all test complete. If you have enough of those tests or if you lower allocated memory with --max_old_space_size, you’ll it running out of memory
Expected behavior
I think the proper behavior would be to tear down environments after a test file each test file is done and allow garbage collector to do it’s job.
Link to repl or repo (highly encouraged)
Please provide either a repl.it demo or a minimal repository on GitHub.
Issues without a reproduction link are likely to stall.
Run npx envinfo --preset jest
Paste the results here:
System: OS: macOS High Sierra 10.13.6 CPU: x64 Intel® Core™ i7-8559U CPU @ 2.70GHz Binaries: Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm npmPackages: jest: 23.6.0 => 23.6.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:12
- Comments:21
To run the
global.gc()
on each test, you just need to putafterEach
(orafterAll
) inside asetupFilesAfterEnv
. This way, the function be called inside each test and after Jest module is initiated.Inside
jest.config.js
:Inside
force-gc.js
:No need to create a particular function on Jest.
I can’t provide example code which repeats this issue easily, as the code isn’t licensed such that I can share it, but my team sees an issue similar to this.
jest
consumes tons of memory and keeps eating memory until it runs into swap space. We’ve been able to fix this by manually runningglobal.gc()
inafterAll
blocks.It looks like something is stopping the garbage collector from running. It might be nice to have a setting in Jest to force the garbage collector to run when there’s only a specified percentage of available memory remaining, or something like that.