 |
CVM Finalization
How does finalization work?
All instances of classes that have a finalize() method are considered to be
finalizable. The only exception is java.lang.Object which has an empty
finalize() method. Classes which have finalizable instances are marked with
a flag to indicate this.
When the class is instantiated, the memory allocator will check for the
finalizable flag. If the class instance is finalizable, then a Finalizer
(extends FinalReference?) instance will be created to refer to this new object.
The Finalizer instance will be added to a list of static list of Finalizers.
Discovering a dead object that needs to be finalized
When the referent is found to be unreachable by hard references, the
Finalizer object (being an extension of FinalReference?) will be enqueued
in the ReferenceHandler? thread's ReferenceQueue?, and it's referent will
be scanned by the GC to keep it alive until after it is finalized.
The ReferenceHandler? thread eventually moved the Finalizer object over to
the Finalizer thread's queue.
Invoking the finalize() method
The Finalizer thread will iterate through it's queue and invoke the finalize()
method on the referents. When the Finalizer object is removed from the queue,
it's next field is set to point to itself. This is the indicator that this
object has been finalized (or will be soon). This ensures that we only call
finalize() on the object once.
-- Main.prasadsanagavarapu - 12 Nov 2006
|