Previous | Next | Trail Map | Reference Objects | Understanding Garbage Collection

Garbage Collection with Reference Objects

How does the introduction of reference objects change things? Unlike ordinary references, the reference in a reference object is treated specially by the garbage collector. A reference object encapsulates a reference to some other object, which is called the referent.

The referent of a reference object is specified when the reference object is created. In the following code, image is an image object passed to sr, a SoftReference object. The image object is the referent of sr, and the reference field in sr is a soft reference to image.

Image image = (sr == null) ? null : (Image)(sr.get());
if (image == null) {
    image = getImage(getCodeBase(), "truck1.gif");
    sr = new SoftReference(image);
}

Object Reachability

Besides strongly reachable and unreachable objects, the Reference objects API gives you strengths of object reachability. You can have softly, weakly and phantomly reachable objects and gain a limited amount of interaction with the garbage collector according to the strength of the object's reachability.

The next lesson explains these strengths and how to use them. This section takes a look at how garbage collection is changed when two of the objects on the heap in the previous diagram are changed to weak reference objects.

When an object is reachable from the root set by some chain of ordinary references (no reference objects involved), that object is said to be strongly reachable. If, however, the only ways to reach an object involve at least one weak reference object, the object is said to be weakly reachable. An object is considered by the garbage collector to be in use if it is strongly reachable. Weakly reachable objects, like unreachable objects, are eligible for collection.

So, weak reference objects let a program maintain a reference to an object without keeping the object from being collected. This way, the program can, for example, perform clean up operations on related objects when a given object becomes weakly reachable. If the garbage collector collects a weakly reachable object, all weak references to it are set to null so the object can no longer be accessed through the weak reference.

The diagram above shows two paths to the object on the heap with the X. It can be reached through the weak reference, and directly from the stack without going through a weak reference object. The object with the X is strongly reachable and not weakly reachable because one path leads to it from the root set without going through a reference object. The garbage collection behavior for this object is the same as other strongly reachable objects until it is no longer reachable from the root set, at which time it becomes weakly reachable.

Here is another way to depict the same idea:

Imagine the root set of references as a solid beam from which objects are suspended by cables (strong references). An object might contain references to other objects. Weak reference objects connect to their referent not by a cable, but by a flexible rubber band, no number of which could support an object. Objects not held by cables fall into the garbage bin below.

The Reference objects API provides several types of reference objects, which gives a program several strengths of object reachability once the referent is no longer strongly reachable. The All About Reference Objects lesson explains the different types of reference objects and what is meant by strengths of reachability.


Previous | Next | Trail Map | Reference Objects | Understanding Garbage Collection