The Artima Developer Community
Sponsored Link

Legacy Design Forum
Finalization and Cleanup



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


order of object collection

Posted by Jeff Benson on September 22, 1999 at 5:43 PM

The arcticle states that there is no guaranteed order with respect to garbage collection. I was wondering, then, if it safe to utilize a reference to an object that is only referred to by the object being garbage collected within a finalize method.

Consider that object A references object B and object B references object C. B's reference to C is the only reference to C, and A's reference to B is the only reference to B. B becomes eligible for garbage collection because A drops its reference to it. Can B, in its finalize clause, pass its reference to C to another object?

I am concerned that the GC might recognize that the only reference to C is an object that is in the process of being
garbage collected (B), and so might garbage collect it before B is garbage collected.

The reason that I want to do this, is that C represents a resource (database connection) that I have in a pool. If the object using the resource gets garbage collected, I want to return C to the pool.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us