I try to understand class ReferenceQueue
It is optional constructor argument for
SoftReference
and
WeakReference
Also it is mandatory argument for PhantomReference
.
According information I have read I can write some thesises
a)for PhantomReference method get always returns null
b)
for Phantom references:
1. gc detect that object can be deleted from memory
2. reference to the object puted to the ReferenceQueue
when we invoke clear or link to reference from queue becaom unreachable and gc see that
3. finalize methods invokes
4. free memory
for weak/soft references:
1. gc detect that object can be deleted from memory
2. finalize methods invokes
3. free memory
4. reference to the object puted to the queue
- When can I pass second argument to
XXXReference
constructor? - Which help I can get?
- Why
PhantomReference
has not constructor withoutReferenceQueue
? - What the reason to have ReferenceQuee which get methods returns null always?
Maybe, the following program helps a bit:
On my system, it prints
or
The order of the first two messages occasionally differs due to the multi-threading. And sometimes, the phantom reference is reported to be enqueued after three polls, indicating that it took more than the specified 100 milliseconds.
The key point is
finalize
method, otherwise they are enqueued after the object has become unreachable againfinalize()
method causes the need of at least one additional garbage collecting cycle to detect that the object is unreachable or phantom reachable againSince more than 99% of all objects don’t need finalization, all JVM vendors are strongly encouraged to detect when
finalize()
has not been overridden or is “trivial”, i.e. an empty method or a solesuper.finalize()
call. In these cases, the finalization step should be elided. You can easily check that this optimization happens in your JVM, by removing thefinalize()
method in the above example. Then it printsSince both are enqueued at once and retrieved in an arbitrary order, the order of the two messages may differ. But they are always both enqueued after one gc cycle.
It’s worth noting that the fact, that phantom references are not automatically cleared, implies that it takes another garbage collection cycle until the object’s memory really can be reused, so the above example requires at least three cycles with the non-trivial
finalize()
method and two without. Java 9 is going to change that, clearing phantom references automatically, so in the above example it will take two cycles with finalization and one without until the memory really can be reclaimed. Well, to be precise, in this simple example the object’s memory will never be reclaimed as the program terminates before that can happen.The code above also demonstrates one of the intended use cases of the Reference API. We can use it to detect when an object’s reachability changed within code under our full control, e.g. using a loop within the
main
method. In contrast,finalize()
may be called by a different, unspecified thread at an arbitrary time. The example also shows that you can draw information from the reference object without needing theget()
method.Practical applications often use subclasses of the reference classes to add more information to them. This is what happens with
WeakHashMap.Entry
which extendsWeakReference
and remembers the hash code and value. The cleanup can be done within the normal map operations, not needing any thread synchronization. This would not be possible with afinalize()
method, besides the fact that the map implementation can’t push afinalize()
method into the class of the keys.This is meant with the “more flexible than finalization” term.
The
WeakHashMap
demonstrates how theget()
method can be useful. As long as the key hasn’t been collected, it will be reported as being in the map and can be retrieved when iterating over all keys or entries.The
PhantomReference.get()
method has been overwritten to always returnnull
to prevent that an application can resurrect the referent of an enqueued reference. This is a direct consequence of the “phantom references are not automatically cleared” rule. That rule itself is questionable and it’s original intention lies in the dark. While the rule is about to be changed in the next Java version, I’m afraid thatget()
will continue to always returnnull
to be backwards compatible.