node.js -- process out of memory

why does this give me FATAL ERROR: JS Allocation failed - process out of memory

arr = []

for (var x=0;x<6000;x++){
    var ys=[];
    arr.push(ys);
    for (var y=0;y<6000;y++){
        var tile = [0,1,2];
        ys.push(tile);
    }
}

the way I see it it's only 108000000 ints, or 432mb, very far away from the 1.4gb limit

Things you are not counting:

  • Overhead of the array objects
  • Size of object references
  • Anything else the program is doing
  • Node.js memory itself

If we assume that object references are 64-bit (not sure of size...), then you have 36 million refs * 4 bytes = 144 megabytes.

You're up to 576 MB before we count the array overhead.

Also, since arrays are resizable, I'm not sure if the length allocated is based exactly on the size, or if the runtime will allocate extra memory just in case.

If I recall, when an ArrayList in Java is expanded, it actually doubles.

I don't know exactly how arrays are implemented in JavaScript, but given that reading array.length is constant time, each array probably has a pre-computed length property.

Since you have 36 million arrays, you have another 144 megabytes.

720 MB so far and counting.

I hope those arrays aren't double-allocated like an ArrayList would do.