RSS Feed for This PostCurrent Article

Java Weak References

It is important that as a Java developer that we understand the life cycle of objects. As quoted from Java documentation,  going from strongest to weakest, the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:

  • An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it.
  • An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
  • An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.
  • An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
  • Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.

Further quote from the Java documentation, weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed they are most often used to implement canonicalizing mappings.

Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references. At the same time it will declare all of the formerly weakly-reachable objects to be finalizable. At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.

Soft and weak references are automatically cleared by the collector before being added to the queues with which they are registered, if any. Therefore soft and weak references need not be registered with a queue in order to be useful, while phantom references do. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.

An implementation of weak references is the WeakHashMap class. WeakHashMap uses weak references for holding map keys, which allows the key objects to be garbage collected when they are no longer used by the application, and the get() implementation can tell a live mapping from a dead one by whether WeakReference.get() returns null. A reference queue is used to remove the entry in this case.

Weak references are most useful for building weak collections, such as those that store metadata about objects only for as long as the rest of the application uses those objects

A simple example illustrates the concept of weak references.

import java.util.Map;
import java.util.WeakHashMap;
public class TestWeakReference {
    public static void main(String args[]){
        Map<String,String> nameValues = new WeakHashMap<String,String>(5);
        String key = new String("name");
        String value = "jason";
        nameValues.put(key, value);
        key = null;

The output


Note I use new String to create the key instead of assigning the value directly. You should understand the difference. If not, read the discussions here and here. In short, if a String exists in the String pool, a reference to that string will be returned but when you use new String(), then a new string is created and returned regardless of its prior existence.

Remember, the example above is just for illustration purpose only.

For weak references, after the key is garbage collected, the referenced object is also garbage collected.

Popularity: 1% [?]

Trackback URL

RSS Feed for This PostPost a Comment