on java soft, weak and phantom reference
ReferenceQueue:
Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.
What does a reachability change mean ?
We know that the Garbage collector will free up any weak reference objects or weakly reachable objects as a part of the garbage collection process. The flow for the same is: If the garbage collector discovers an object that is weakly reachable, the following occurs:
- The WeakReference object's referent field is set to null, thereby making it not refer to the heap object any longer.
- The heap object that had been referenced by the WeakReference is declared finalizable.
- The WeakReference object is added to its ReferenceQueue. Then the heap object's finalize() method is run and its memory freed.
So what is this ReferenceQueue ? When creating non-strong reference objects we have the option of passing a reference queue as a part of the Reference constructor. As seen from the above explanation, this reference queue is a way for the GC to inform the program that a certain object is no longer reachable.
Reference Objects and Garbage Collection
非常好的文章,从垃圾收集与各种引用的关系来说明,非常清晰透彻
java gc的可达性说明
弱引用说明
强弱共存
Java Garbage Collection - Understanding Phantom Reference with examples