question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

  1. create a bunch of Angular components with tests using “ng g c my-test-cmp-1”, “…cmp-2”, etc
  2. run “jest --runInBand --logHeapUsage components/” so it runs all the tests
  3. 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.
  4. 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:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:21

github_iconTop GitHub Comments

10reactions
dav1appcommented, Jan 27, 2020

To run the global.gc() on each test, you just need to put afterEach (or afterAll) inside a setupFilesAfterEnv. This way, the function be called inside each test and after Jest module is initiated.

Inside jest.config.js:

{ 
  setupFilesAfterEnv: [
    '<rootDir>/force-gc'
  ]
}

Inside force-gc.js:

afterEach(() => {
  if (global.gc) global.gc()
})

No need to create a particular function on Jest.

7reactions
w0rpcommented, Jun 21, 2019

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 running global.gc() in afterAll 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Your Jest Tests are Leaking Memory
Fortunately it's easy to tell if your tests are leaking. Run your tests with node --expose-gc ./node_modules/.bin/jest --runInBand -- ...
Read more >
My Jests tests are leaking memory, how can I fix this?
First of all, make sure that you have leaky tests by running jest with the option: jest --logHeapUsage. Run your tests and check...
Read more >
Dominic Elm ⚡️ ‍♂️ on Twitter: "#Jest is nearly unusable ...
Without the flag, a single (simple) test takes 10s to run..... ... But still, shouldn't be this slow, and especially not leak memory....
Read more >
Jest CLI Options
Logs the heap usage after every test. Useful to debug memory leaks. Use together with --runInBand and --expose-gc in node. --maxConcurrency=<num> ...
Read more >
Finding the cause of a memory leak in Jest tests
Running tests with heap usage recording. We tried increasing memory allocation to see if the tests just take too much memory, but in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found