|State the behavior that is guaranteed by the garbage collection system.|
How and when garbage collection occurs is left up to the implementation of the JVM, all you can know is that an object becomes eligible for garbage collection when there are no longer any references to it. At that stage, no variable points to the object, therefore your code has no way of accessing it and the object can safely be removed by the system.
finalize() method of a class is called before it is garbage collected. The method takes no arguments and must
void, and throws
Throwable. Do not confuse the
finalize() method (garbage collection)
finally keyword (exception handling).
|Write code that explicitly makes objects eligible for garbage collection.|
My definition of explicit differs, as I would categorize this as implicit:
Date d = new Date(); d = null;There is no longer a reference pointing to the Date created on the first line, therefore that object is eligible for garbage collection. No matter what you do, there is no way your code could get back a reference to that object, so your code cannot be affect by its removal. If on the other hand you had passed the object into a Collection, or it was pointed to by some class, instance or local variable somewhere, then it is not ready for garbage collection (for example you could pass it to a method or constructor before setting d to null).
Date d = new Date(); d = new Date(0);The new Date created on the second line holds the value midnight, January 1, 1970. The previous object referred to by
d, which held the current
Dateis now eligable for garbage collection, but the new value of
d(1/1/1970) is unaffected.
|Recognize the point in a piece of source code at which an object becomes eligible for garbage collection.|
An object is eligible for garbage collection when there are no longer any references to it. This can happen if a variable
assigned to a newly created object, but later is assigned to
null or a different object, so there is nothing
pointing to the original object. Or when a method exits, all the local variables go out of scope, so all the objects are eligible
for garbage collection, unless there is a reference to them somewhere outside the method.
©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".