Pvvd Prasad
Pvvd Prasad

Reputation: 209

nodejs memory allocation failure

I was using decryption to decrypt "MySql" data. I got the below issue:

<--- Last few GCs --->
31681 ms: Mark-sweep 654.1 (666.5) -> 492.5 (509.8) MB, 267.5 / 0.0 ms [allocation failure] [GC in old space requested].
31839 ms: Mark-sweep 492.5 (509.8) -> 492.2 (506.8) MB, 157.5 / 0.0 ms [allocation failure] [GC in old space requested].
31985 ms: Mark-sweep 492.2 (506.8) -> 492.2 (497.8) MB, 146.2 / 0.0 ms [last resort gc]. 32122 ms: Mark-sweep 492.2 (497.8) -> 492.2 (497.8) MB, 136.8 / 0.0 ms [last resort gc]. <--- JS stacktrace --->

What is it regarding and how to fix it,

Thanks in advance

Upvotes: 20

Views: 62507

Answers (7)

Nqobani Nhlengethwa
Nqobani Nhlengethwa

Reputation: 1

I recently faced this issue and downgrading node to 14.15.3 fixed this for me.

From your project root dir, run the following.

nvm install 14.15.3 (assuming you don't already have it installed)

nvm use 14.15.3

delete your node modules and install them again.

Upvotes: 0

Gabrielizalo
Gabrielizalo

Reputation: 946

I was caught in the same situation. When I ran my ReactJS project in my WSL (Windows 11), I received the same problem.

The other alternatives did not work for me.

As mentioned in How to solve JavaScript heap out of memory error I resolved the issue in my WSL using ZSH by adding the line next lint to my .zshrc file.

export NODE_OPTIONS=--max_old_space_size=4096 #4GB

Upvotes: 0

Leo Orotoma Ojigbo
Leo Orotoma Ojigbo

Reputation: 45

I recently encountered a similar error. After days of trying out all solutions online without success, I finally found what worked for me. I had the Node js LTS version of 16.7 installed on my machine. Upgrading to the current version of node js 18.0 solved the issue.

Upvotes: 2

Zeeshan Safdar
Zeeshan Safdar

Reputation: 457

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.

Upvotes: 4

Ihor
Ihor

Reputation: 3809

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
    

Upvotes: 10

ali
ali

Reputation: 1099

I had this problem but by removing globally installed electron and installing electron locally for my project this error disappears.

Upvotes: -2

Cryptic Pug
Cryptic Pug

Reputation: 567

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!

Upvotes: 18

Related Questions