0

I am using Node.js. I have the following javascript code.

var length=9980;
this.distance= [];//hold distance
this.cost= [];   //hold time cost   

 console.log("before create distance and cost arrays"); 
 console.log("length" + length);   
 for(var i=0; i < length;i++)
 {   
    console.log("creating cost : " + i );       
    this.distance[i] = new Array(length);
    this.cost[i] = new Array(length);
 }; 

By this, I want to create 2 dimension array of

 distance, cost

as shown above.

The problem there is error reported.

enter image description here

Array should be able to hold millions of elements, but there is such error.

What is the problem? How can I make it work?

halfer
  • 19,824
  • 17
  • 99
  • 186
arslan
  • 2,034
  • 7
  • 34
  • 61
  • 1
    maybe same problem: http://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory – pL4Gu33 Dec 23 '16 at 19:32
  • Alim, I notice that you commonly add "please help" to your questions. Additions of this kind do not make questions more likely to be answered, and it may be interpreted as a form of begging. Please do also refrain from asking for urgency - that is not acceptable when addressing volunteers. Thank you! – halfer Dec 23 '16 at 23:32
  • It is already at at least `2 * (9,980 + 1) * 7,740 = 154,505,880` array elements there. That *is* millions. Those two arrays alone would take about 312 MB in Firefox. – Robert Mar 15 '17 at 20:30

1 Answers1

3

I think you are running into node's default memory limits.

Try adding running your node app with the --max_old_space_size= flag.

node --max_old_space_size=4096 app.js

According to http://prestonparry.com/articles/IncreaseNodeJSMemorySize/ the number is in megabytes so this should give you a memory cap of 4GB.

etchesketch
  • 821
  • 4
  • 14
  • Actually, I tried that and did not work(even I set size as 10000). an array [9800][9800] may be to big. Looks like I have to remove these arrays and find out some way which uses less memory. – arslan Dec 23 '16 at 19:40
  • 2
    The issue you are running into is probably because you are creating a two dimensional array. If you can make this work in a flat, one dimensional array, you will be way better off. An array of arrays is going to use a massive amount of memory. For each of those 9980 array elements, you are creating two additional 9980 arrays each with elements, and that seems to be a bit excessive. Try to handle all of this in one array or one object if possible. – jaggedsoft Dec 23 '16 at 19:44