loading...

Reducing Jest Memory Usage

pustovalov_p profile image Pavel Pustovalov ・2 min read

Sometimes this problem occurs on the CI:

Summary of all failing tests
  ● Test suite failed to run

    ENOMEM: not enough memory, read

      at Runtime.readFile (node_modules/jest-runtime/build/index.js:1880:21)

Environment

node 12.16.3
yarn 1.22.4
jest 26.0.1

Circle CI container 2 CPU / 4 GB RAM

Earlier tests were run by this command

yarn jest app/javascript --forceExit --ci -w=2

for debugging, run the tests in one thread:

node ./node_modules/.bin/jest app/javascript --forceExit --ci -w=1

Test Suites: 100 passed, 100 total
Tests:       1163 passed, 1163 total
Snapshots:   186 passed, 186 total
Time:        89.236 s

Maximum RAM usage (full log):

[56660:0x103fe3000]    91511 ms: Scavenge 1465.3 (1535.5) -> 1459.2 (1539.8) MB, 12.4 / 0.0 ms  (average mu = 0.974, current mu = 0.951) allocation failure 

If run specs with expose-gc and logHeapUsage parameters, you will see a decrease in RAM usage:

node --expose-gc ./node_modules/.bin/jest app/javascript --forceExit --ci -w=1 --logHeapUsage

Test Suites: 100 passed, 100 total
Tests:       1163 passed, 1163 total
Snapshots:   186 passed, 186 total
Time:        95.85 s

Maximum RAM usage (full log):

[57108:0x103fe3000]    18741 ms: Scavenge 533.1 (552.0) -> 524.6 (555.7) MB, 5.1 / 0.0 ms  (average mu = 0.988, current mu = 0.979) allocation failure 

With these two parameters Jest will run garbage collection (source):

if (globalConfig.logHeapUsage) {
  if (global.gc) {
    global.gc();
  }
  result.memoryUsage = process.memoryUsage().heapUsed;
}

Conclusion

Run node with expose-gc and jest with logHeapUsage parameters, can decrease maximum memory consumption without increasing tests execution time, in my case:

1539.8 MB -> 555.7 MB

Discussion

pic
Editor guide
Collapse
victorkurauchi profile image
Victor Kurauchi

Nice one. thanks for sharing !