DEV Community

Pavel Pustovalov
Pavel Pustovalov

Posted on

Reducing Jest Memory Usage

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)

Enter fullscreen mode Exit fullscreen mode

Environment

node 12.16.3
yarn 1.22.4
jest 26.0.1

Circle CI container 2 CPU / 4 GB RAM
Enter fullscreen mode Exit fullscreen mode

Earlier tests were run by this command

yarn jest app/javascript --forceExit --ci -w=2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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

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

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
Enter fullscreen mode Exit fullscreen mode

Oldest comments (2)

Collapse
 
victorkurauchi profile image
Victor Kurauchi

Nice one. thanks for sharing !

Collapse
 
vostrik profile image
Pavel Vostrikov

Thanks for the article and link to nice code from Jest. It helps us to reduce memory usage on CI.