[Bug]: Jest main process memory usage grows unboundedly with v8 coverage #14287
Description
Version
29.5.0/main
Steps to reproduce
- Have a large codebase, with many test files that import much of the codebase.
- Run the tests with the v8 coverage reporter
(I can create a repro here, but the root cause is clear enough that I didn't bother.)
Expected behavior
If I run N tests which import a large codebase, the jest main process memory usage should grow slowly if at all.
Actual behavior
Each test file executed increases the jest main process memory usage by quite a lot -- to the point where running with coverage enabled on our whole codebase would use 16GB or more (that's where I gave up).
Additional context
The issue here is that the main process records the v8 coverage output separately for every test file that it runs, and only merges them at the end. The code that keeps them is here:
and then the reports don't get merged until here (called in
onRunComplete
):jest/packages/jest-reporters/src/CoverageReporter.ts
Lines 431 to 433 in aaf5315
For a large codebase, these outputs can be 100MB or more, because they include, among other things, the source of every imported file (and, if source maps are enabled, the pre-transpilation source too), as well as some representation of its control-flow graph.
My assumption is that the best way to fix this is to merge as we go, like the babel coverage reporter does, although I don't know if that would have too much perf cost. (Maybe merge whenever total number of yet-unmerged coverage report items is >10k or something?)
I suspect this may be behind #5837, and perhaps other issues with memory usage with --coverage
, but of course it's hard to tell.
Environment
System:
OS: macOS 13.4
CPU: (10) x64 Apple M1 Max
Binaries:
Node: 16.17.1 - /nix/store/fyaisfdhkviz6sb628qxlmy5wcasqfhq-nodejs-16.17.1/bin/node
npm: 8.12.1 - ~/.local/bin/npm
Activity