Daniel Pitts said:
I don't know. Most likely you will know when you need to use it when
you come across something you can't do any other way.
I interpret the Javadoc's explanation as "Phatom References allow you
to clean up after objects are ready to be garbage collected, without
the risk of reintroducing the refered Object back into the reachable
object graph (and therefore delaying garbage collection of that object).
The differences between PhantomReference and WeakReference are:
1. The phantom reference is not queued until the object has been finalized,
while the weak reference may be queued before finalization.
2. Weak references are cleared by the GC before being queued, while phantom
references are not.
3. It isn't possible to get an object from a phantom reference (get() always
return null), while it is possible to get an object from a weak reference.
(Not after the weak reference in enqueued, of course: see 2.)
4. A phantom reference much be part of a queue, while a weak reference need
not be.
4 follows from 3: a phantom reference that's not in a queue would be
useless. Otherwise, the differences can be summarized as:
o - A weak reference tells you whether the object is active or not. If
get() returns true, it's active. If not (and it never will after being
enqueued), it's inactive, though there's no way to tell whether it's
finalizable, finalized, or deleted.
o - A phantom reference, by being enqueued, tells you specifically that the
object has been finalized but not yet deleted.
I can *almost* see why I'd want to know that the object has been finalized.
I have no idea why I'd care that it hasn't been deleted yet.