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?