Four objects are eligible for garbage collection when i3 = null;
is executed in the class shown below. I've added comments to explain how I got this answer. Is my reasoning correct?
public class Icelandic extends Horse{
public void makeNoise(){
System.out.println("vinny");
}
public static void main(String args[]){
/**
* 2 objects created
*/
Icelandic i1 = new Icelandic();
/**
* 2 objects created
*/
Icelandic i2 = new Icelandic();
/**
* 2 objects created
*/
Icelandic i3 = new Icelandic();
/**
* i3 is now pointing at i1, original Icelandic() referred to by i3 now
* has no reference - 2 objects now have no reference
*/
i3 = i1;
/**
* i3 is now pointing at i1, original Icelandic() referred to by i1 now
* has no reference - 2 objects now have no reference
*/
i1 = i2;
/**
* Total of six objects created, 4 objects no longer have a reference so 4
* can be garbage collected.
*
* Setting to null below doesn't make any difference to garbage collector
* as objects now do not have a reference
*/
i2 = null;
i3 = null;
}
}
interface Animal {
void makeNoise();
}
class Horse implements Animal{
Long weight = 1200L;
public void makeNoise() {
System.out.println("whinny");
}
}
These are steps of your program:
Icelandic i1 = new Icelandic();
Icelandic i2 = new Icelandic();
Icelandic i3 = new Icelandic();
i3 = i1;
i1 = i2;
i2 = null;
i3 = null;
So the last diagram concludes that only 2 objects are ready for garbage collection.
I hope I am clear. You can see object names as references to the objects.
EDIT:
As said by BalusC, Long weight = 1200L is also object. So 2 more objects each for i1 and i3 are eligible or garbage collections. So in all 4 objects are eligible or garbage collection.
As a very simple rule of thumb, an object in java can be garbage collected if the program's behaviour would not change if all the fields of the object were copied to local variables ( an optimising program transformation ) and all references to the object were set to null.
Quoting 'The Java VM Spec'
12.6.1 Implementing Finalization
Every object can be characterized by two attributes: it may be reachable, finalizer-reachable, or unreachable, and it may also be unfinalized, finalizable, or finalized.
A reachable object is any object that can be accessed in any potential continuing computation from any live thread. Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner.
Discussion
Another example of this occurs if the values in an object's fields are stored in registers. The program may then access the registers instead of the object, and never access the object again. This would imply that the object is garbage.
So in your case, as none of the references to any of the Icelandic
objects are dereferenced, all of them may be garbage collected immediately . As nothing dereferences i1
to i3
, an optimising compiler is at liberty to elide everything after i3 = new Icelandic()
as a no-op and collect all six objects immediately.