Tutorial Description - Eligibility for garbage collection
An object is eligible for garbage collection when no live thread can access it. An object can become eligible for garbage collection in different ways.
Eligibility for garbage collection page 2 of 5
An object is eligible for garbage collection when no live thread can access it.
An object can become eligible for garbage collection in different ways:
If the reference variable that refers to the object is set to null, the object becomes eligible for garbage collection, provided that no other reference is referring to it.
If the reference variable that refers to the object is made to refer to some other object, the object becomes eligible for garbage collection, provided that no other reference is referring to it.
Objects created locally in a method are eligible for garbage collection when the method returns, unless they are exported out of the method (that is, returned or thrown as an exception).
Objects that refer to each other can still be eligible for garbage collection if no live thread can access either of them.
Consider the following example: