46

On Server startup exporting 2GB(Approximately) data from mongodb to redis,then getting error as FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory.

Then started server with this command node --max-old-space-size=4076 server.js and works fine. But needs to configure in nodejs applicaton so that node server always starts with 4gb memory. Please help me how to fix this? Thanks.

Ahmad Baktash Hayeri
  • 5,802
  • 4
  • 30
  • 43
anil
  • 653
  • 1
  • 5
  • 14
  • just out of curiosity, is there something that is stopping you from putting this in an npm start script? – James LeClair Dec 18 '15 at 14:30
  • Hi James, i am a newbie to nodejs. can you please tell me where exactly i need to keep this? can you provide sample code? i am very much grateful to you. – anil Dec 18 '15 at 14:36
  • I posted an answer with some more details than I could have provided in a comment. Hope it helps you along. – James LeClair Dec 18 '15 at 14:47

4 Answers4

43
node --max-old-space-size=8192 some-script.js 
  • Where SomeScript is the file name that you want to execute using node.
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
M T Head
  • 1,085
  • 9
  • 13
32

one option: npm start scripts

https://docs.npmjs.com/misc/scripts

These are added to your package.json under the "scripts" section

{
  //other package.json stuff

  "scripts":{
     "start": "node --max-old-space-size=4076 server.js"
  } 

}

then to run it call npm start instead of typing in node + args + execution point.

Note: if you name it something other than start, npm run [yourScriptNameHere] will be the command to run it

This is a better option than trying to reconfigure node to use 4gb by default (don't even know if its possible tbh). It makes your configuration portable by using the baked in methods as it stands and allows others who encounter your code in the future to understand this is a need.

James LeClair
  • 1,234
  • 10
  • 12
  • @InzamamMalik not that I know of - or could find on a quick google search. I believe this is by design also. The original post had a use case that could have been solved streaming data from one endpoint to the other and not buffering it all in memory. Chances are if you need to override node (or really, v8) memory limits all the time, there's probably an architectural issue with your implementation. If you still feel that you need it for every process, the start script really doesn't seem that inconvenient. – James LeClair Oct 26 '17 at 15:13
  • 2
    @InzamamMalik - you can do this globally on a system by saving the following environment variable - `NODE_OPTIONS=--max-old-space-size=4096` – caesay Feb 07 '19 at 17:48
25

You can also set NODE_OPTIONS when running an npm script rather than node itself:

"scripts": {
  "start": "NODE_OPTIONS=--max-old-space-size=4096 serve",
},
Kevin Cooper
  • 5,018
  • 4
  • 37
  • 51
5

There are two ways to resolve this, First way is to set memory size explicitly for your application, already answered above. And the other way is to set memory size globally for node - This is done by creating an Environment variable, with

variable name=NODE_OPTIONS

and

variable value=--max-old-space-size=4096
Kailash Uniyal
  • 878
  • 1
  • 10
  • 14
Prashant K
  • 869
  • 9
  • 9