Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Chrome, why are TypedArrays not on the JS Heap?

If I deliberately waste memory using JS arrays in Chrome (v48), by calling this repeatedly:

var rootReference;
function wasteBunchOfMemoryWithJsArrays()
{
    rootReference = rootReference || [];
    for (var i = 0; i < 1000; i++)
    {
        var blah = [];
        for (var j = 0; j < 10000; j++)
        {
            blah.push(j);
        }

        rootReference.push(blah);
    }
}

Then the browser tab crashes at about 700MB usage (according to Task Manager), which roughly matches the limit specified in performance.memory.jsHeapSizeLimit.

However if I waste memory using a TypedArray such as Int32Array, by calling this repeatedly:

var typedRootReference;
function wasteBunchOfMemoryWithInt32Arrays() 
{
    typedRootReference = typedRootReference || [];
    for (var i = 0; i < 100; i++) {
        var blah = new Int32Array(100000);

        for (var j = 0; j < 100000; j++) {
            blah[j] = j;
        }

        typedRootReference.push(blah);
    }
}

Then I can keep going up and up until I hit what I presume to be the Windows 32bit process memory limit around 2.9GB!

Is it simply the case that TypedArrays get around the JS heap limitations? Why? Is there something else I should be concerned about with TypedArrays?

like image 617
Brendan Hill Avatar asked Sep 25 '22 07:09

Brendan Hill


1 Answers

The data stored in typed arrays, because it is all numbers, is guaranteed not to contain any references to other objects, so it has no effect on whether any other object is garbage. Therefore, the data does not ever need to be scanned, copied, or otherwise manipulated by the garbage collector.

Given this, it is a very common implementation strategy to place such “pure” “bulk” data outside of the garbage collector's heap, to reduce the amount of data the garbage collector must manage.

There is no fundamental reason for this to mean that there should be two separate memory limits (or lack of limits), as you observed, but if no effort is made to impose a combined memory limit, that's what happens.

like image 88
Kevin Reid Avatar answered Sep 29 '22 07:09

Kevin Reid