node.jsmemory-managementmemory-leaks

How to use a lot of memory in node.js?


I am working on a system designed to keep processes behaving properly and I need to test its memory usage kill switch. I am yet to find a way to quickly and efficiently use a lot of system memory quickly with node.js. I have tried using arrays but it seems as if the garbage collector compresses them. Basically, I need to deliberately cause a fast memory leak.

EDIT: I need to reach around 4 gigabytes of used system memory by one process.


Solution

  • If garbage collector is compressing your array, maybe you are pushing always the same value inside it.

    you should try shomething like

    const a = []
    for (let i = 0; i < 9000000000; i++)
        a.push(i)
    

    You also should run your process with a flag to increase memory limit. Note that with a limit of 9B, your array should be bigger than 4GB and your process will exit with non zero status code.

    node --max_old_space_size=4096 yourscript.js