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?

3
  • 1
    maybe same problem: stackoverflow.com/questions/38558989/node-js-heap-out-of-memory Commented Dec 23, 2016 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! Commented Dec 23, 2016 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. Commented Mar 15, 2017 at 20:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.