nodejs memory allocation failure

Alllocate more memory to your script by using the following argument to node: --max_old_space_size=x

Example:

node --max_old_space_size=8000 yourscript.js

This will allocate about 8GB to your script. Eventually this is still not sufficient and you should decrypt your SQL in smaller chunks and make use of your physical drive instead of RAM memory.

Hope this helps!


I was caught in the same issue. When I run the project, it was React js Project, I got the same error.

<--- Last few GCs --->
...
JavaScript heap out of memory

etc After 2 hours of research and connecting with my peers, I downgrade my node js version from v14.15.4 to v14.15.3. Restart my PC, And then delete my node_modules and yarn.lock file, and reinstall my packages, run my project, and the error is gone. Maybe it will help some others facing the same issue.


Proper solution

Something is using too much memory in your node.js app. This is usually bad sign and requires investigation in long term. To find out the exact piece of code, which does this - you may want to check out node.js profiling techniques. Some article about this https://nodejs.org/en/docs/guides/simple-profiling/

Fast solution

In some cases fast workaround is what we want. For such cases, as correctly pointed out by Cryptic Pug - we can increase JS memory allocation limit. There are few options how to do so (based on your needs)

  1. pass argument to node directly
    node --max_old_space_size=4096 app.js
    
  2. use environment variable
    NODE_OPTIONS=--max_old_space_size=4096
    node app.js
    
  3. use .npmrc file. Put following in .npmrc local or global file
    node-options=--max_old_space_size=4096
    
    and run your script
    node app.js