Java Reference
In-Depth Information
FIGURE 1.9 One GregorianCalendar object has no references to it and the other now
has three.
This object is eligible for
garbage collection.
christmas
day
25
month
12
year
d
2009
newyears
day
1
month
1
year
2010
Setting christmas to null on line 11 does not cause the object from line 7 to become
eligible for garbage collection because there are still two references pointing to it: d and
newyears . However, after line 12 the main method ends and both d and newyears go out of
scope. Therefore, the object instantiated on line 7 becomes eligible for garbage collection
after line 12.
Know When an Object Is Eligible for Garbage Collection
The GCDemo program is typical of a question that you will encounter on the certifi cation
exam. Make sure you understand exactly when each of the two GregorianCalendar
objects becomes eligible for garbage collection.
What does it mean to become eligible for garbage collection? Why not simply have the
garbage collector immediately free the memory instead? The answer is that there is no
guarantee in Java as to exactly when an object is actually garbage collected. The JVM
Search WWH ::




Custom Search