Objects are garbage collected when no live thread can access then. One way of achieving that is by nulling all the references to that object.
But that doesn't work always:
There are still references to the Test objects however no live thread can access them. The JVM recognizes these Islands of Objects and GC'd them.
wouter@wouter-laptop:~/temp$ javac Test.java
wouter@wouter-laptop:~/temp$ java -verbose:gc Test
[GC 232K->120K(32256K), 0.0009260 secs]
[Full GC 120K->103K(32256K), 0.0037630 secs]
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Sep 19, 2008
By making reference to null, you are telling JVM that i do not need this object anymore and you may garbage collect it at your own convenient time unless the no more reference is given to this object else where(this includes finalized method).