Soft, Weak, Phantom Reference

Published by

on

java.lang.ref package provides soft reference, weak reference and phantom reference as a class. The objects that are created from those classes are considered specially by GC which makes the developers to interact with GC with a limited access.

Weak Reference

Figure 1. Example of creating WeakReference object

java.lang.ref.WeakReference class creates a WeakReference object by encapsulating the object that it is going to refer to.

As seen from the Figure 1, the WeakReference object that encapsulates the Sample object refers to the Sample object that it is referring to. The below Figure 2 describes how the reference is composed.

Figure 2.

And the last line of the code that makes ex = null; makes the Sample object to be only referred by the WeakReference object, which makes the Sample object as “weakly reachable”.

The objects that are created by SoftReference, WeakReference, PhantomReference classes are usually called as “reference object“. The term is only used for those 3 class objects and not used for the objects are considered to be making a “strong reference“.

Reference and Reahability

From the early Java, application code was not able to interfere the GC’s activities. GC would just seek the “unreachable” objects for clean up. However, after the java.lang.ref package, developers could distinguish strongly reachable, softly reachable, weakly reachable, and phantomly reachable objects so that the GC can act different per cases.

Figure 3. Reachable, Unreachable, Weakly Reachable Objects

The green objects are the ones that’s only referred by the WeakReference objects. GC will collect those weakly reachable objects as well as the unreachable objects for clean up and retrieve their memories. Even though the objects are referred from the Root Set of References, it enables the GC to clean themselves up.

As seen from the Figure 3, the Weak Reference objects itself is considered to be Strongly Reachable Objects as it is referred from the Root Set, and the object marked as A is a Strongly Reachable Object due to the same reason.

When GC considers an object as weakly reachable, it sets the reference as null, which makes that object to be considered the same as unreachable object.

Strengths of Reachability

Figure 4. Strong Reference, Soft Reference, Weak Reference
  • Strong Reference
    • Reference objects that are usually pointing the objects created by new(). GC excludes the strong reference objects.
  • Soft Reference
    • Acts just like Weak Reference, except that it is only likely to be cleaned up by GC when it’s running out of memory. If there are plenty of memory available, GC would not clean up the memory.
  • Weak Reference
  • Phantom Reference
    • Phantom Reference Objects will be put into ReferenceQueue and postpones the memory clean up. Objects that are phantomly reachable will not set as null by GC, so the application code should use clear() method to clean it up.

Reference

One response to “Soft, Weak, Phantom Reference”

  1. […] Soft, Weak, Phantom Reference […]

    Like

Leave a comment