Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript garbage collection of typed arrays in Chrome

Please consider the following javascript. I would have thought that the allocation in the loop would allow for garbaging collection to kick in preventing to overflow the heap. It rightly does so in Firefox, however in Chrome (tested on OSX) the snippet crashes the open tab after several iterations.

    for (var i = 0; i < 1024; ++i) {
            // Allocate a 16mb buffer
            var buffer = new Float32Array(1024*1024*4);

            // Try to explicitly mark it for free by either
            delete buffer;
            // or
            buffer = null;

            console.log(i);
    }

In itself this script it not all that useful. But I am trying to optimize my Javascript application in order for it to use less memory. Therefore I'd like your opinion. Is this a bug in Chrome? Do you know of any workarounds to explicitly call the garbage collection during code execution (in FF and IE they seem to exist)? Thanks!


Edit: There appears to exist a button on the Chrome Inspector called "Collect Garbage". It is the 7th button on the lower bar on the "Timeline" panel of the Inspector. Doesn't this signify that a way exist to call GC from Javascript? After all, aren't parts of the Inspector written in Javascript?

like image 289
user1556435 Avatar asked Sep 07 '12 12:09

user1556435


People also ask

Is JavaScript garbage collected?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC).

How do I force garbage collection in JavaScript?

You can't force garbage collection (not in any sane fashion). If your variables aren't going out of scope automatically, just set them to null . Show activity on this post. Best advice is to define them in scope that makes them eligible for garbage collection.

Are JavaScript arrays typed?

JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.

What is garbage collection and how is it performed in JavaScript apps?

When it comes to programming, Garbage Collection means cleaning the memory spaces which don't contain useful data and then reallocating those cleared memory to some other data which is both active and useful. That is the basic process of Garbage Collection in pretty much all the programming languages in the world.


1 Answers

This is pure speculation, but I wonder whether the garbage collection is being postponed until the the current item on the run loop finishes executing.

If so, then perhaps it would work if you shaped it like this:

var i = 0;
function allocateArray() {
    var buffer = new Float32Array(1024*1024*4);
    if (++i < 1024) {
        setTimeout(allocateArray, 0); // Post the next call to the run loop
    }
}
allocateArray();
like image 193
cloudfeet Avatar answered Oct 08 '22 11:10

cloudfeet