jest: Memory leak when using --coverage flag

bug

What is the current behavior?

When running our test suites with --coverage memory seems to leak, and heap size increases with ~13MB per test suite (we have a total of 240 test suites), ultimately causing Jest to slow down (as mentioned in other memory leak issues)

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Unfortunately we can’t provide the actual code from our tests, but all we have to do is enable --coverage to get the above behavior.

What is the expected behavior?

We would expect some overhead, but 13MB per test suite is causing our CI to timeout and make the coverage flag unusable.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

node@6.9.4, yarn@0.19.1, jest@19.1.0-alpha-eed82034, Mac OSX v10.12.3

  "jest": {
    "setupFiles": [
      "<rootDir>/src/setup.js"
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/jest-setup.js",
    "roots": [
      "<rootDir>/src"
    ],
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 60,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    }
  }

I’ve masked the real path… so this is actually different files:

node --expose-gc $(BIN)/jest --no-cache --i --logHeapUsage

PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (72 MB heap size)
PASS  $/__tests__/test.js (72 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (74 MB heap size)
PASS  $/__tests__/test.js (71 MB heap size)
PASS  $/__tests__/test.js (72 MB heap size)
PASS  $/__tests__/test.js (78 MB heap size)
PASS  $/__tests__/test.js (78 MB heap size)

node --expose-gc $(BIN)/jest --coverage --no-cache --i --logHeapUsage

PASS  $/__tests__/test.js (139 MB heap size)
PASS  $/__tests__/test.js (151 MB heap size)
PASS  $/__tests__/test.js (164 MB heap size)
PASS  $/__tests__/test.js (177 MB heap size)
PASS  $/__tests__/test.js (189 MB heap size)
PASS  $/__tests__/test.js (202 MB heap size)
PASS  $/__tests__/test.js (215 MB heap size)
PASS  $/__tests__/test.js (228 MB heap size)
PASS  $/__tests__/test.js (240 MB heap size)
PASS  $/__tests__/test.js (253 MB heap size)
PASS  $/__tests__/test.js (265 MB heap size)
PASS  $/__tests__/test.js (278 MB heap size)
PASS  $/__tests__/test.js (291 MB heap size)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 19 (9 by maintainers)

Most upvoted comments

I’ll try to make a small repro, but probably won’t have the time before Tuesday.

We are reducing memory for coverage in Jest 21 (jest@test) but further leaks are probably not something we’ll look at anytime soon. If you’d like to investigate further, please send us PRs with fixes 😃 Thank you.

@danieljuhl any news on the repro?

@danieljuhl any updates? Can you also try if it happens on Jest 20?

I can repro it with the Jest repo, although I get a similar behavior without --coverage

With coverage

❯ node --expose-gc ./packages/jest-cli/bin/jest.js --coverage --no-cache --i --logHeapUsage packages
 PASS  packages/jest-util/src/__tests__/FakeTimers-test.js (55 MB heap size)
 PASS  packages/jest-matchers/src/__tests__/matchers-test.js (67 MB heap size)
 PASS  packages/jest-haste-map/src/__tests__/index-test.js (69 MB heap size)
 PASS  packages/jest-config/src/__tests__/normalize-test.js (71 MB heap size)
 PASS  packages/pretty-format/src/__tests__/ImmutablePlugins-test.js (78 MB heap size)
 PASS  packages/jest-mock/src/__tests__/jest-mock-test.js (81 MB heap size)
 PASS  packages/pretty-format/src/__tests__/React-test.js (85 MB heap size)
 PASS  packages/jest-cli/src/__tests__/SearchSource-test.js (92 MB heap size)
 ...
 ...
 ...
 PASS  packages/jest-editor-support/src/__tests__/parsers/BabylonParser-test.js (403 MB heap size)
 PASS  packages/jest-test-typescript-parser/src/__tests__/TypeScriptParser-test.js (420 MB heap size)
 PASS  packages/jest-resolve-dependencies/src/__tests__/__fixtures__/file-test.js (423 MB heap size)
 PASS  packages/jest-resolve-dependencies/src/__tests__/__fixtures__/related-test.js (425 MB heap size)
 PASS  packages/jest-jasmine2/src/__tests__/it_to_test_alias-test.js (424 MB heap size)

Without coverage

❯ node --expose-gc ./packages/jest-cli/bin/jest.js --no-cache --i --logHeapUsage packages
 PASS  packages/jest-util/src/__tests__/FakeTimers-test.js (45 MB heap size)
 PASS  packages/jest-matchers/src/__tests__/matchers-test.js (47 MB heap size)
 PASS  packages/jest-haste-map/src/__tests__/index-test.js (43 MB heap size)
 PASS  packages/jest-config/src/__tests__/normalize-test.js (47 MB heap size)
 PASS  packages/pretty-format/src/__tests__/ImmutablePlugins-test.js (48 MB heap size)
 PASS  packages/jest-mock/src/__tests__/jest-mock-test.js (47 MB heap size)
 PASS  packages/pretty-format/src/__tests__/React-test.js (47 MB heap size)
 PASS  packages/jest-cli/src/__tests__/SearchSource-test.js (54 MB heap size)
 PASS  packages/pretty-format/src/__tests__/pretty-format-test.js (58 MB heap size)
 PASS  packages/jest-cli/src/reporters/__tests__/VerboseReporter-test.js (59 MB heap size)
 PASS  packages/jest-runtime/src/__tests__/transform-test.js (68 MB heap size)
 PASS  packages/jest-haste-map/src/crawlers/__tests__/watchman-test.js (71 MB heap size)
 ...
 ...
 ...
 PASS  packages/jest-editor-support/src/__tests__/parsers/BabylonParser-test.js (243 MB heap size)
 PASS  packages/jest-test-typescript-parser/src/__tests__/TypeScriptParser-test.js (256 MB heap size)
 PASS  packages/jest-resolve-dependencies/src/__tests__/__fixtures__/file-test.js (255 MB heap size)
 PASS  packages/jest-resolve-dependencies/src/__tests__/__fixtures__/related-test.js (254 MB heap size)
 PASS  packages/jest-jasmine2/src/__tests__/it_to_test_alias-test.js (251 MB heap size)