Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to increase nodejs default memory?

Tags:

node.js

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.

like image 878
anil Avatar asked Dec 18 '15 12:12

anil


People also ask

What is Node js default memory limit?

In Node < 12 , it sets a limit of 1.5 GB for long-lived objects by default. If this exceeds the memory available to your dyno, Node could allow your application to start paging memory to disk.

What is the default max old space size for Node?

By default, the memory limit in Node. js is 512 MB. To increase this amount, you need to set the memory limit argument —-max-old-space-size . It will help avoid a memory limit issue.

How many GB is Node js?

Background. Out of the box, a 64-bit installation of node. js assumes a memory ceiling of 1.5GB per node process. If you are running your node app in a memory constrained environment, e.g. a low-cost VPS server or PaaS instance, it's necessary to inform the v8 runtime that you have a reduced memory ceiling.

What is RSS memory Nodejs?

rss , or resident set size, refers to the amount of space occupied in the main memory for the process, which includes code segment, heap, and stack.


2 Answers

node SomeScript.js --max-old-space-size=8192 
  • Where SomeScript is the file name that you want to execute using node.
like image 69
M T Head Avatar answered Sep 24 '22 08:09

M T Head


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.

like image 24
James LeClair Avatar answered Sep 22 '22 08:09

James LeClair