Jest appears unaffected by node --max-old-space-size option
Asked Answered
L

2

20

I have been working to overcome apparent memory leaks when running jest that are described here: https://github.com/facebook/jest/issues/7874

I would like to just give jest a whole bunch of heap space so that my tests will complete in spite of the leaks. However, using this command line:

node --max-old-space-size=8192 --expose-gc ./node_modules/.bin/jest --no-cache --runInBand --logHeapUsage

I still get "Javascript heap out of memory" at around 1500MB of heap usage. If I run:

node --max-old-space-size=8192

and then do:

> v8.getHeapStatistics()
{
   ...
   heap_size_limit: 8640266240
   ...
}

so clearly I can affect node's heap size. But it somehow is not affecting jest.

Node: 16.13.0 Jest: 27.4.4 Any thoughts?

Lockjaw answered 10/12, 2021 at 15:54 Comment(3)
Did you ever get this sorted out? I've got the same problem.Maldon
Never gotten it sorted out, no. We can't currently run our jest tests in our nightly builds because of the Jest memory leak. We've tried the Jest patch, but even that does not consistently work. We are frustrated.Lockjaw
I recommend using node v18. We had a memory consumption problem which disappeared when I switched to v18.Reglet
P
2

Perhaps one of these settings in your jest.config.js can fix it for you:

{
    maxWorkers: 1,
    globals: {
        'ts-jest': {
            isolatedModules: true
        }
    }
}

I think I remember having the same issue as you, and I solved it with all this configuration. Not sure which part was the difference-maker, but just give it a try, I would say.

Parity answered 30/5, 2023 at 14:26 Comment(1)
Worked an absolute treat, thank you! I have no idea why though!Pragmatist
B
0

I think you can fix your problem setting --max-old-space-size=8192 to a less value. Probably you are stealing memory from V8 process while it running jest.

A reference for your problem but more generic.

Bighead answered 27/7, 2023 at 20:2 Comment(1)
I have plenty of memory (well, 16GB on the machine). I have a function that takes well over 2GB (the default allocated) of memory. I know I can afford 4 or 8 GB. The problem here is that the jest test runner is running in some node process (probably a node.vm) that we a looking for a way to allow it more memory.Destination

© 2022 - 2025 — McMap. All rights reserved.