Reference

In the view of garbage collector, there are two categories of objects: reachable or not reachable. The garbage collector would recover the memory from unreachable objects since the unreachable objects are no longer "useful".

Reachable objects are classified into four categories:

  • strongly reachable, an object is strongly reachable if it can be reached without traversing any Reference objects.
  • softly reachable, an object is softly reachable if it is not strongly reachable but can be reached by traversing a SoftReference object.
  • weakly reachable, an object is weakly reachable if it can be reached by traversing a WeakReference object.
  • phantom reachable, an object is phantom reachable if it can be reached by a PhantomReference object.

Unlike strongly reachable objects, softly, weakly, and phantom reachable objects can be garbage collected.

softly reachable, weakly reachable and phantom reachable are stored in SoftReference, WeakReference, and PhantomReference repectively.

Reference is the abstract superclass of SoftReference, WeakReference, and PhantomReference subclasses. ReferenceQueue is a concrete class whose instances describe queue data structures.

Reference is a generic type Reference<T>, where T identifies the referent's type. ReferenceQueue is declared as generic type ReferenceQueue<T>, where T identifies the referent's type.

Home 
  Java Book 
    Language Basics  

Reference:
  1. Reference
  2. SoftReference
  3. WeakReference
  4. PhantomReference