Java Reference
In-Depth Information
When an object is instantiated, it is in the unfinalized state. For example,
Employee john = new Employee();
The object referred to by the john reference variable is in an unfinalized state after the above statement is
executed. The finalizer of an unfinalized object had never been invoked automatically by the JVM. An object becomes
finalizable when the garbage collector determines that the finalize() method can be invoked on the object.
A finalized object has its finalize() method invoked automatically by the garbage collector.
Based on reachability, an object can be in one of three states:
Reachable
Finalizer-reachable
Unreachable
An object is reachable if it can be accessed through any chain of references from the root set. A finalizer-reachable
object can be reached through the finalizer of any finalizable object. A finalizer-reachable object may become
reachable if the finalizer from which it is reachable stores its reference in an object that is reachable. This is the
situation when an object resurrects. An object may resurrect itself in its finalize() method or through another
object's finalize() method. An unreachable object cannot be reached by any means.
There are nine combinations of object states based on their finalization status and reachability status. One of the
nine combinations, finalizable and unreachable, is not possible. The finalize() method of a finalizable object may
be called in future. The finalize() method can still refer to the object using this keyword. Therefore, a finalizable
object cannot also be unreachable. An object can exist in one of the following eight states:
Unfinalized - Reachable
Unfinalized - Finalizer-reachable
Unfinalized - Unreachable
Finalizable - Reachable
Finalizable - Finalizer-reachable
Finalized - Reachable
Finalized - Finalizer-reachable
Finalized - Unreachable
Weak References
Java 2 introduced the concept of weak references in Java garbage collection by including a new package called
java.lang.ref . The concept of weak references in the context of garbage collection is not new to Java. It existed
before in other programming languages, but Java included it in Java 2. So far, the object references I have discussed are
strong references. That is, as long as the object reference is in scope, the object it refers to cannot be garbage collected.
For example, consider the following object creation and reference assignment statement:
Employee john = new Employee("John Jacobs");
Here, john is a reference to the object created by the expression new Employee("John Jacobs") . The memory
state that exists after executing the above statement is depicted in Figure 11-2 .
 
Search WWH ::




Custom Search