On Nov 30, 2009, at 7:30 AM, Stephan Beal wrote:
> This is a _sorely_ missing feature in v8. Maybe if you and i keep complaining loudly enough about it, they'll eventually give us a way to force our destructors to be called.
Timely release of external references after a context is disposed is important for Chrome as well, as those references hold onto entire DOM trees, which get very large. I've been looking into this as part of the memory task-force on Chromium, and there are some recent and upcoming commits that should help here.
That said, I think a second issue has crept into this discussion: of disposing external refs when V8 exits completely. As mentioned earlier, this shouldn't generally be necessary because V8 exits when the process exits, in which case most external resources get torn down along with the process (file handles, sockets, etc.)
This can be an issue with apps that need to tear down V8 without exiting the process. In that case, though, the native code could keep its own collection of all native resources that are bound to V8 objects, and after closing down V8 it can iterate over all remaining resources and close them.
Another alternative (I believe) would be to repeatedly call V8's low-memory handler until it returns false, which should wring out all possible collectible memory, including weak references.
> The fact that v8's performance characteristics are based solely off of Chrome's needs is, IMO, philosophically wrong. One specific app should not determine the destiny of all applications which link to v8. Believe it or not, Chrome is the minority case for v8 - it's used in many, many more applications that Chrome, but yet every single one of them suffers with this limitation because it is useful for Chrome.
I'm sure you're correct as measured by number of projects (there's surely more than one other project in the world using V8), but I'm pretty sure you're incorrect as measured by number of active users (Chrome is up to 30 million last I heard.)
This is a classic dilemma of open source projects: the design is driven by some consensus of committers. We (Chromium) run into the same issues with WebKit, which has had to adjust from being "Safari's back-end" to supporting multiple browsers with different needs. If a lot of people have a need for V8 to work differently, then they can work on designing and implementing a modification to V8 to allow it to work that way. I can't speak for the V8 team, but if such a patch is architecturally clean and doesn't affect performance for clients that don't need it, it ought to be accepted.
> It is not only important, but CRITICAL for certain types of bound objects, e.g. database handles which must release resources cleanly. Almost all of the classes i've bound so far REQUIRE a destructor call in order for their behaviours to be well-defined. e.g. an sqlite3 database handle, a (FILE*) handle, etc. i've always got to add lots of extra code to my apps to ensure they are cleaned up.
Hm. It's usually considered a truism of garbage collection that finalizers are not guaranteed to be called, and that resources that must be cleaned up should be cleaned up manually. The Java VM specification, for example, explicitly does not guarantee invocation of finalizers.
It is not necessary to close a FILE* explicitly when your process exits; the OS will close the file handle for you. You may need to flush the FILE* if it has unwritten buffered output, but that is the kind of thing you should really be doing manually anyway: what if you write a bunch of stuff without flushing, and then your process keeps running for several days? That data hasn't been written to the file, so anyone looking at the file will see partial data, and in the event of a crash the file will be corrupt.
—Jens