title: Node.js heap memory limit for single object
body: <p>Does v8 have limits on the heap allocations for single objects?</p>
  
  <p><code>a = new Array(1024*1024*102)</code></p>
  
  <p>fails on node command-line with </p>
  
  <p><code>FATAL ERROR: JS Allocation failed - process out of memory</code></p>
  
  <p>Also, this fails with the same error when run as a script</p>
  
  <p><code>node --expose-gc --nouse-idle-notification  --max-old-space-size=8192</code></p>
  
  <p><code>FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory</code></p>
  
  <pre><code>var util = require('util');
  o = {};
  
  while(1) {
      o["hahahahahaha" + String(ctr)] = ctr;
      ctr++;
  
      if (ctr % 100000 === 0) {
          console.log(util.inspect(process.memoryUsage()));
          if (ctr % 10000000 === 0) gc();
      }
  }
  </code></pre>
  
  <p>Last output:</p>
  
  <p><code>{ rss: 1009557504, heapTotal: 993408824, heapUsed: 964980592 }</code></p>
  
  <p>However,</p>
  
  <pre><code>var a = [];
  while(1) {
      var o = {};
      o["hahahahahaha" + String(ctr)] = ctr;
      a.push(o);
      ctr++;
  
      if (ctr % 100000 === 0) {
          console.log(ctr);
          console.log(util.inspect(process.memoryUsage()));
          console.log();
          if (ctr % 10000000 === 0) gc();
      }
  }
  </code></pre>
  
  <p>is just fine</p>
  
  <p><code>{ rss: 5466140672, heapTotal: 1091224368, heapUsed: 1070460592 }</code></p>
  
  <p>Edit:</p>
  
  <p><code>node -v</code></p>
  
  <p><code>v0.10.25</code></p>
  
  <p><code>uname -a</code></p>
  
  <p><code>Linux 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux</code></p>
  
  <p>Edit 2:
  Even this works!
  It seems that v8's limit applies to number of properties an object can have?</p>
  
  <pre><code>while(1) {
  
      if (!o["hahahahahaha" + String(Math.floor(ctr/1000000))]) {
          o["hahahahahaha" + String(Math.floor(ctr/1000000))] = {};
          console.log(Object.keys(o))
      }
      o["hahahahahaha" + String(Math.floor(ctr/1000000))][String(ctr)] = ctr;
      ctr++;
  
      if (ctr % 100000 === 0) {
          console.log(ctr);
          console.log(util.inspect(process.memoryUsage()));
          console.log();
          if (ctr % 10000000 === 0) gc();
      }
  }
  </code></pre>
  
  <p><code>{ rss: 2474512384, heapTotal: 2466405768, heapUsed: 2431583008 }</code></p>
  
  <p>Also, I found this:
  <a href="https://github.com/v8/v8/blob/master/src/objects.h#L2928" rel="noreferrer">https://github.com/v8/v8/blob/master/src/objects.h#L2928</a></p>
  
  <p>I wonder if it's relevant.</p>