[gclist] Finalization releasing resources

Giuliano Carlini GCARLINI@us.oracle.com
27 Mar 97 11:09:02 -0800

"John D. Mitchell" <johnm@non.net> said:
It is Bad Practice(tm) to rely on finalization for reclaiming resources
which are heavily contended for (file handles) or otherwise where
timeliness is important (e.g., sockets).

Why do you say this?

If you don't use a finalizer to release resources, then how do you release resources? Do you
explicitly call a release function. If so, doesn't that have the same problems as
explicitly releasing memory?

I'd suggest that you can get around issues of scarcity in some cases by
virtualizing/abstracting the resource. The abstract objects cooperate in sharing the
pool of scarce resources. When a virtualized resource object needs one of the scarce
objects and none are available, it will request it from one of the objects which currently
posseses a scarce resource. One of the current owners will save any state necessary, and
then give up the scarce resource. The virtualizing object obtaining the scarce resource
then restores any state necessary, and uses the resource. This works for things like
files, but obviously wouldn't work for everything; semaphores for example.

Why is releasing a socket quickly important? How does this place additional constraints
on a system which releases sockets by invoking a finalizer on reclaim beyond those on a
system using explicit release? Is it once more an issue of scarcity?