Skip to content

[Bug]: Jest main process memory usage grows unboundedly with v8 coverage #14287

Open
@benjaminjkraft

Description

Version

29.5.0/main

Steps to reproduce

  1. Have a large codebase, with many test files that import much of the codebase.
  2. 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:

this._v8CoverageResults.push(testResult.v8Coverage);

and then the reports don't get merged until here (called in onRunComplete):
const mergedCoverages = mergeProcessCovs(
this._v8CoverageResults.map(cov => ({result: cov.map(r => r.result)})),
);

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions