V8 does not clean up all garbage
Asked Answered
S

1

10

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

Slr answered 30/5, 2014 at 19:6 Comment(0)
B
6

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.

Beaut answered 8/4, 2015 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.