Tom Anderson wrote:
i believe a PhantomReference is likely to tell you about collection sooner
than a finalizer would.
markspace wrote:
The opposite. Finalizers
might
get run as soon as the object becomes eligible for GC, on the first GC pass
or later.
Then the object is freed up on the second pass,
or later,
then "sometime maybe later" the PhantomReference is enqueued.
The Javadocs for 'finalize()' point out that after that method runs,
"no further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can be
accessed by any thread that has not yet died, including possible actions by
other objects or classes which are ready to be finalized, at which point the
object may be discarded."
Since there is no guarantee that other finalizers will run at any given GC
pass, it might take a few passes after a particular object's has run before
the GC *may* (not definitely will) discard it.
Maybe using both is the solution. And since objects become weakly
two passing-out brains -- R. Beef Kazenzakis