V8 total cleanup

354 views
Skip to first unread message

Florent S.

unread,
Oct 27, 2012, 8:59:10 AM10/27/12
to v8-u...@googlegroups.com
Hi there !

I would like to know if there is a way to force V8 to release ALL the objects in memory.

After I executed my script, I would like to free all memory that has been allocated during the script in the embedded object.
I already the "MakeWeak" mechanism and it works when V8 does a garbage collection. But if my script is like :

// begin
var wnd = new Window();
// end

The C++ object embedded in Window will never been destroyed, resulting of course memory leaks.

I've tried to do a v8::V8::Dispose() but it doesn't help.

Is there any way to totally clean up the memory without exiting my application ?

Best regards,
Florent

P.S: My application is under Windows 32b

Yang Guo

unread,
Oct 29, 2012, 4:58:57 AM10/29/12
to v8-u...@googlegroups.com
What you want is to dispose the context and create a new one. You can find an example for this in d8.cc. There should be no cross-context memory leaks (if you find one, report it!).

In the specific case of your example, (if understood it correctly) the property "wnd" of the global object is set to the new Window object, retaining it in memory. GC won't touch it since the object is reachable through the global object. Setting wnd to undefined will break that link and cause the GC to step in eventually.

Yang

Florent S.

unread,
Oct 30, 2012, 7:05:12 AM10/30/12
to v8-u...@googlegroups.com
Thanks Yang.

That's what I suspected.
If I wnd = null; at the end of my script GC() clean up everything.

So I added this after my script run :

HObject o = GlobalContext->Global();
HArray a = o->GetPropertyNames();
for (int i = a->Length() -1; i >= 0; i--)
{
HValue k = a->Get(i);
HValue v = o->Get(k);
o->Set(k, Null());
}
while(!V8::IdleNotification()) {} // GC

V8::IdleNotification cleans up few thing but not my Window C++ object whereas wnd is null ...

So eventually, I run a clean up script after my first one :

for (var i in this) {
this[i] = null;
}

And at last, every thing is cleaned up !

Thanks for the support,
Florent

Yang Guo

unread,
Nov 5, 2012, 3:33:56 AM11/5/12
to v8-u...@googlegroups.com
I still think that what you want is to create a new context and dispose the old one.

Yang

Florent S.

unread,
Nov 5, 2012, 6:12:48 PM11/5/12
to v8-u...@googlegroups.com
Nope.

I want to dispose the old one, call GC and get everything cleaned up.

Regards,
Florent

Paul Harris

unread,
Dec 3, 2012, 10:48:15 AM12/3/12
to v8-u...@googlegroups.com
Hi Yang and Florent,

This is exactly what I am experiencing...
any property (eg wnd) on the global object is NOT cleaned up by GC, even after the context has been disposed.

Has anyone reported this as a bug?

ps, Florent: I think the reason your C++ o->Set() loop didn't work as well as the JS version, is that you need to wrap the whole loop in a HandleScope.
You are grabbing Handles to the values in the global object, but you aren't releasing them before calling the GC.

cheers,
Paul

Paul Harris

unread,
Dec 3, 2012, 10:49:19 AM12/3/12
to v8-u...@googlegroups.com
ps, Florent: I think the reason your C++ o->Set() loop didn't work as well as the JS version, is that you need to wrap the whole loop in a HandleScope.
You are grabbing Handles to the values in the global object, but you aren't releasing them before calling the GC.



So your loop should work better like so:
{
HandleScope scope;
HObject o = GlobalContext->Global();
HArray a = o->GetPropertyNames();
for (int i = a->Length() -1; i >= 0; i--)
{
HValue k = a->Get(i);
// DO not grab this handle HValue v = o->Get(k);
o->Set(k, Null());
}
} // end of handlescope here 

while(!V8::IdleNotification()) {} // GC
 

Paul Harris

unread,
Dec 3, 2012, 11:49:50 AM12/3/12
to v8-u...@googlegroups.com
I have had some luck WITHOUT the loop, with this:

context.Dispose();
V8::ContextDisposedNotification(); // not sure if required
V8::LowMemoryNotification(); // not sure if required
while (!V8::IdleNotification());

// and I also do another idlenote loop just before I exit the isolate scope, just in case.

My problem was that I was not releasing a handle to a global scope, or rather, I was trying to GC before the handlescope around the global object had tidied up.
Reply all
Reply to author
Forward
0 new messages