Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

V8 does not clean up all garbage

I'm having trouble cleaning up garbage in V8. First, my Javascript is as follows:

var bigstring = "ASD";
for (var b = 0; b < 20; b++) {
    bigstring = bigstring + bigstring;
}
trace("bigstring " + bigstring.length);

function frame() {
    // generate some garbage
    var junkArray = [];
    for (var i = 0; i < 1000; i++) {
        junkArray.push(i + bigstring);
    }
}

From C++, I'm running a loop:

  • Call frame.
  • Collect garbage: while(!V8::IdleNotification()) {};

The expected result is that each iteration, the junkArray garbage is collected. After all, IdleNotification only returns true when "V8 has done as much cleanup as it will be able to do" (doc).

In fact, the garbage is only cleaned up* approx. every 100 iterations. Am I missing a step? Is junkArray for some reason not garbage immediately after frame?

*Determined by comparing before-and-after heap usage

like image 669
Bart van Heukelom Avatar asked May 30 '14 19:05

Bart van Heukelom


Video Answer


1 Answers

Instead of v8::V8::IdleNotification, try using v8::Isolate::LowMemoryNotification. As far as I can tell that's the only way to get recent V8 builds to do a full GC via the public API.

like image 75
BitCortex Avatar answered Oct 20 '22 18:10

BitCortex