help icon indicating copy to clipboard operation
help copied to clipboard

Weird behavior with allocationg memory in node v10.4.0

Open ghost opened this issue 8 years ago • 4 comments

  • Node.js Version: v10.4.0
  • OS: Windows 10

Hello,

I noticed weird behavior in my Node.js application: my system can allocate up to 32GB of memory, but my Node.js process is crashes when it reaches about ~1.5GB

Example 1:

var a = [];
while(1){
  a.push(Buffer.allocUnsafe(1e9));
}

Performance graph 1:

You can zoom image to see numbers better. Basically, it allocates about 30GB and then throws buffer allocation failed error, which is ok.


Example 2:

var a = [];
while(1){
  a.push(new class{constructor(){
    this.a = 'a'.repeat(1e6);
  }});
}

Performance graph 2:

In this example, the program crashes when it reaches about 1.5GB and the error is fatal:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed
- JavaScript heap out of memory

I'm filing issue here because I'm not sure if this is an issue, or I'm doing something wrong, so if it issue, I can file it node repo, for now I would like to hear from you is this an issue in V8's gc?

Obviously, both references to class objects and strings and buffers are stored in heap, so if Node.js is capable to allocate 32GB for buffers, why it isn't capable to allocate more than 1.5GB for classes, objects and strings?

Thanks.

ghost avatar Jun 11 '18 13:06 ghost

--max_old_space_size=<size in mb> is what you're looking for, it defaults to ~1500.

edit: I probably win a price for fasted answered question ever.

bnoordhuis avatar Jun 11 '18 13:06 bnoordhuis

Thanks bnoordhuis! I must admit it is very useful command line option, I didn't know about it. I tested and it seems to work fine.

But wait, why then I can allocate 32GB for buffers, if max old space is still 1500 megabytes?? I mean, it seems very counter-intuitive to me..

ghost avatar Jun 11 '18 14:06 ghost

Buffers are backed by off heap storage, and thus in total just constrained by the address space of your process and other practical limits under OS control.

Strings are stored in the v8 heap, and thus affected by the 'old' heap size option that 'bnoordhuis' supplied.

shellberg avatar Jun 11 '18 14:06 shellberg

thanks shellberg

ghost avatar Jun 11 '18 16:06 ghost