nodejs out of memory

。_饼干妹妹 提交于 2019-12-18 10:22:45

问题


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


回答1:


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.




回答2:


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



回答3:


I don't get a memory allocation error when I run your script. How much RAM is on your system?

Edit Ok with the author's updated notes, I can replicate it.

Node is trying to convert your entire array to a string. The array is 16777216 elements long. Each element contains a number at least 9 digits long. Converting that to a string 150,994,994 characters long. Its just a huge operation that is exceeding the memory capabilities of node.



来源:https://stackoverflow.com/questions/7357339/nodejs-out-of-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!