Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs out of memory

I came across a curious issue today. This may be an easy answer for others, but it has me stumped. Why does the code below cause a memory error?

var cur = 167772160; var bcast = 184549375; var addresses = []; while (cur <= bcast){   cur += 1;   addresses.push(cur); } addresses.length  addresses // memory goes from a few megs to over a gig in seconds when trying to print this 

I get one of these two errors...the first when i run this code in node's interpreter and the latter when i run it through nodeunit:

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

FATAL ERROR: JS Allocation failed - process out of memory

like image 841
Sneaky Wombat Avatar asked Sep 09 '11 05:09

Sneaky Wombat


People also ask

How do I fix a process out of memory in Node js?

This exception can be solved by increasing the default memory allocated to our program to the required memory by using the following command. Parameters: SPACE_REQD: Pass the increased memory space (in Megabytes).

Why does Node run out of memory?

A common problem while working on a JavaScript Node. js project is the “JavaScript heap out of memory” error. This error usually occurs when the default memory allocated by your system to Node. js is not enough to run a large project.

How do I give Node js more memory?

Solution. The solution to run your Node. js app with increased memory is to start the process with an additional V8 flag: --max-old-space-size . You need to append your desired memory size in megabytes.

How much RAM does Nodejs need?

256 MB is sufficient amount of RAM to run Node. js (e.g. on Linux VPS instance), assuming no other memory-hog software is run.


2 Answers

You can increase the default limits by passing --max-old-space-size=<value> which is in MB.

The example will allow node's heap use up to 4GB (4096 megabytes) of memory:

node --max-old-space-size=4096 app 
like image 59
Saheed Avatar answered Sep 29 '22 13:09

Saheed


It happens when I try to access the array. But getting the length does not.

> var cur = 167772160; > var bcast = 184549375; > var addresses = []; > while (cur <= bcast){ ...   cur += 1; ...   addresses.push(cur); ... } 16777216 > addresses.length  16777216 > addresses FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory 

Here's another SO question, memory limit in Node.js (and chrome V8) that relates to issue with memory usage.

like image 44
ace Avatar answered Sep 29 '22 15:09

ace