Java Reference
In-Depth Information
How are weak references different from strong references? The difference lies in how the garbage collector treats
them. Weak references do not prevent the objects from being collected by the garbage collector. That is, if there is a
weak reference to an object, the garbage collector can still reclaim the object. However, if there is at least one strong
reference to an object, the garbage collector will not reclaim the object. Before you start looking at details of how to
use these three reference classes in Java programs, let's discuss the reachability of an object when these classes are
involved in a program.
Strongly reachable : An object is strongly reachable if it can be reached from the root set
through at least one chain of references, which does not involve any weak reference.
Softly reachable : An object is softly reachable if it is not strongly reachable and it can be
reached from the root set through at least one chain of references, which involves at least one
soft reference, but no weak and phantom references.
Weakly reachable : An object is weakly reachable if it is not strongly and softly reachable and
it can be reached from the root set through at least one chain of references, which involves at
least a weak reference and no phantom references.
Phantom reachable : An object is phantom reachable if it is not strongly, softly, and weakly
reachable and it can be reached from the root set through at least one chain of references,
which involves at least a phantom reference. A phantom reachable object is finalized, but not
reclaimed.
Among the three kinds of weak references, a soft reference is considered stronger than a weak reference and a
phantom reference. A weak reference is considered stronger than a phantom reference. Therefore, the rule to identify
the reachability of an object is that if an object is not strongly reachable, it is as reachable as the weakest reference in
the reference chain leading to that object. That is, if a chain of references to an object involves a phantom reference,
the object must be phantom reachable. If a chain of references to an object does not involve a phantom reference, but
it involves a weak reference, the object must be weakly reachable. If a chain of references to an object does not involve
a phantom reference and a weak reference, but it involves a soft reference, the object must be softly reachable.
How do you determine the reachability of an object when there is more than one chain of references to the
object? In such cases, you determine the object's reachability using all possible chains of references and use the
strongest one. That is, if an object is softly reachable through one chain of references and phantom reachable through
another, the object is considered softly reachable. Figure 11-5 depicts the examples of how an object's reachability
is determined. The elliptical shape at the end of every reference chain represents an object. The reachability of the
object has been indicated inside the elliptical shape. The rectangles denote references.
 
Search WWH ::




Custom Search