way to GC whole context?

20 views
Skip to first unread message

ondras

unread,
Feb 24, 2009, 11:26:56 AM2/24/09
to v8-users
Hi,

I have the following scenario:

- a JS class / function is defined in C++; this class represents DB
connection.
- I have a persistent handle to that class's instance.
- I make the handle weak, wanting to be informed when GC collects it:
this way, I can close the DB connection when it is no longer used.

This solution works, provided that GC gets a chance to clean the
(lost) reference. However, I want to make sure that the MakeWeak
callback gets executed _always_ (at least when my script ends and
Context gets deleted), because after execution ends, I don't want any
DB connections hanging around. Is there some way to "purge" whole
global object / context so everything gets GC'ed ?


Thanks a lot,
Ondrej Zara

Ondrej Zara

unread,
Feb 25, 2009, 1:31:28 PM2/25/09
to v8-users
Bump... is there any V8 dev that can confirm that one cannot truly
guarantee that GC of an object will ever occur?


O.


2009/2/24 ondras <ondre...@gmail.com>:

Alex Iskander

unread,
Feb 25, 2009, 1:38:01 PM2/25/09
to v8-u...@googlegroups.com
As far as I know, there is no (supported) way to do this.

There are other ways around it, however. For instance, you can keep a
list of the database connections that are open in the current context,
and when you get rid of the context, just go through that list and
close them.

I ran into a similar problem with files; although the memory and the
handle to the file would be destroyed by the operating system, the
stream's "flush" command would not be called, and as such, the output
would be incomplete. I rigged up an auto-deleter that basically
allowed me to say:
//on object creation
autoDestroy(theObject);
//on object deletion
cancelAutoDestroy(theObject);

The auto deleter would then be triggered when the program exited.

Alex
Alex Iskander
Web and Marketing
TPSi




Ondrej Zara

unread,
Feb 26, 2009, 2:05:23 AM2/26/09
to v8-u...@googlegroups.com
Hi Alex,

> As far as I know, there is no (supported) way to do this.

thanks for response. I was afraid of this :-(

I will probably file this as a bug, because it seems logical to me
that when a JS context is destroyed, all data objects in it should get
GC'ed. What is your opinion?

O.

Søren Gjesse

unread,
Feb 26, 2009, 7:53:43 AM2/26/09
to v8-u...@googlegroups.com
Take a look at http://codereview.chromium.org/27201. It runs a full GC when a new context is created and contexts have been destroyed since a new context was created the last time.

This is still somewhat experimental, as adding such GC heuristics sometimes have unexpected negative results. For this change the reason for postponing the GC until a new context is created is due to the fact that in Chromium each frame has its own context, and leaving a page with say 10 iframes would cause 10 GC's which is not desired.

Regards,
Søren

Alex Iskander

unread,
Feb 26, 2009, 8:10:51 AM2/26/09
to v8-u...@googlegroups.com
I see. And you have to do real garbage collection, and not just delete all the objects in a context, because an object could exist in more than one context. The garbage collection would then occur globally, which could cause a slowdown.

What about a manual cleanup function? This could, perhaps, be potentially useful for Chromium; it could be called when a frame is closed, but only be called once if the whole process is being closed.

Alex

Søren Gjesse

unread,
Feb 26, 2009, 10:31:56 AM2/26/09
to v8-u...@googlegroups.com
The objects in the heap have no knowledge of which context was active when they where created, and also as you mention an object created for one context might become referenced from another context. Therefore it requires a full GC to process weak handles.

There is a GC extension which can be enabled through the flag --expose-gc. With this extension enabled calling "gc()" from JavaScript performs a full GC. However be careful GC can be expensive, and running to many GC's without much garbage cost.

Regards,
Søren
Reply all
Reply to author
Forward
0 new messages