Weird behavior with allocationg memory in node v10.4.0
- 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.
--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.
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..
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.
thanks shellberg