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.
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.
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.
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.
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.
node SomeScript.js --max-old-space-size=8192
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With