Reputation: 863
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.
Upvotes: 66
Views: 134221
Reputation: 12368
If you want to increase the memory usage of the node globally, you can export environment variable, like this:
export NODE_OPTIONS=--max_old_space_size=4096
or 8GB:
export NODE_OPTIONS=--max_old_space_size=8192
Upvotes: 9
Reputation: 1290
node --max-old-space-size=8192 some-script.js
Upvotes: 64
Reputation: 909
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
Upvotes: 5
Reputation: 5375
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",
},
Upvotes: 32
Reputation: 1294
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.
Upvotes: 37