What memory limits exist in Web Browsers?

What memory limits exist in Web Browsers?
0.0 0

#1

Web browsers like Chrome, Firefox, even Node.js all seem to have some unspecified memory limit. Hitting this limit will cause a script to stop executing. In the spirit of learning I’d like to know more about these limits, what they are, and how to write better code to avoid hitting them in real life applications.


Example Code:
(WARNING: This code will exhaust your javascript execution environment’s memory by creating a huge array filled with random number, running this on an older web browser may freeze your computer, newer ones may lag or crash, be cautious when testing this code and do so on an isolated environment).

'use strict';
const myArr = [];
for(let i = 0; i < 99999999999999999999999; i++){
  myArr.push(Math.random())
}
myArr.sort()
console.log(myArr)

My Tests:

Chrome:

Firefox:

Tor Browser:
(crashed entirely - no error message to show)
Update: On a second test it gave me an error similar to Mozilla Firefox

NodeJS 9.4.0 on Amazon Linux (AWS Cloud9):


<--- Last few GCs --->

      47 ms: Scavenge 2.9 (10.0) -> 2.7 (10.0) MB, 0.5 / 0.0 ms [allocation failure].
      48 ms: Scavenge 3.5 (10.0) -> 3.0 (12.0) MB, 0.1 / 0.0 ms [allocation failure].
     506 ms: Mark-sweep 477.2 (486.0) -> 383.8 (393.6) MB, 3.9 / 0.0 ms (+ 2.1 ms in 2 steps since start of marking, biggest step 2.1 ms) [GC interrupt] [GC in old space requested].
     916 ms: Mark-sweep 621.5 (987.8) -> 240.1 (606.3) MB, 9.7 / 0.0 ms [allocation failure] [GC in old space requested].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x179271bcf781 <JS Object>
    2: /* anonymous */ [/home/ec2-user/environment/playground/testing.js:~1] [pc=0x26f1b7951aa8] (this=0x3e6e4466fcd1 <an Object with map 0x2c2bea207959>,exports=0x3e6e4466fcd1 <an Object with map 0x2c2bea207959>,require=0x3e6e4466fe49 <JS Function require (SharedFunctionInfo 0x3e6e4466c6b1)>,module=0x3e6e4466fdc1 <a Module with map 0x354646e1cb99>,__filename=0x3e6e4466fd79 <String[48]: /home/e...

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
 1: node::Abort() [node]
 2: 0x10a03cc [node]
 3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
 5: v8::internal::Heap::AllocateUninitializedFixedDoubleArray(int, v8::internal::PretenureFlag) [node]
 6: v8::internal::Factory::NewFixedDoubleArray(int, v8::internal::PretenureFlag) [node]
 7: 0xc52ad0 [node]
 8: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 9: 0x26f1b78079a7
Aborted

#2