Finalization Problem

The finalization problem is a problem of many languages which support GarbageCollection. Many such languages provide finalizers - methods to be called when the object is to be discarded. (The "final" method in JavaLanguage, etc.) However, the presence of these can complicate GC semantics, for the following reasons:

Some claim finalizers aren't useful (or shouldn't be) in a purely garbage-collected system where resources are not scarce, that might be true. In practical systems, there are many resources (files, other kernel objects, distributed thingies) which a) are outside the scope of the GarbageCollector (though it's good practice to wrap these in objects as soon as possible); b) sufficiently scarce that the inderministic behavior of a GarbageCollector is an inappropriate management strategy; c) have explicit "cleanup" semantics besides the releasing of resources (such as flushing buffers when closing files). Hence, finalizers.

It should be noted that C++ destructors are similar, but not the same. C++ destructors are deterministic when it comes to when they are executed, and handle many more things (such as memory management).

Interestingly enough, sometimes finalizers are not enough. Java has finalizers, but many objects implement a DestroyMethod which performs resource de-allocation and subsequently invalidates the object (see ObjectInvalidation). Client code is expected to call destroy() when appropriate (a finalizer is provided which does this automatically - assuming it is run - should the client forget).

Possible solutions to the FinalizationProblem.

See also:

DeterministicFinalization FinalizeInsteadOfProperDestructor DeterministicResourceManagement


EditText of this page (last edited June 4, 2007) or FindPage with title or text search