Garbage collection in Java is to discard objects that are no longer needed and to reclaim their resources. A Java object is subject to garbage collection when it is out of scope of the control flow of the program.
Does Java garbage collection guarantee that a program will not run out of memory?
No. Java Programs may use up memory resources faster than they are garbage collected. A Java program can create objects that are not subject to garbage collection.
System.gc() is just a convenience for Runtime.getRuntime().gc().
However, if you are genuinely a beginner, you should never be calling either of these methods.
99.9% of the time, Java will garbage-collect as appropriate and does not need you to tell it to do so. Calling one of the gc() methods does not force garbage collection to happen, either; it only suggests to the JVM that now might be a good time for some garbage collection.
Example
· Usually if an object doesnot have any references then it is eligible for Garbage collection.
Sometimes, even though object having some reference still eligible for garbage collection
(The best example for above situation is Island Of Isolation)
Here is a sample code
Class Test
{
Test t;
public static void main(String args[]){
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
//No Object Is Eligible for GC
t1.t=t2; //No Object Is Eligible for GC
t2.t=t3; //No Object Is Eligible for GC
t3.t=t1; //No Object Is Eligible for GC
t1=null;
//No Object Is Eligible for GC (Coz t2 still have a reference)
t2=null;
//No Object Is Eligible for GC (Coz t3 still hava a reference)
t3=null;
//All the 3 Object Is Eligible for GC (None of them have a reference)
}
Thought of explaning pictorially but am unable to paste a pic here…. :(
Island of Isolation…
Now carrying forward your sample code (with few addition) to better understand the garbage collection.
here in the above code(previous code) if we want to check how many objects are eligible for Garbage Collection (practically) then we can add finalize method in the above code, as…
/******** Garbage collection *********/
Class Test
{
Test t;
static int i=1;
protected void finalize()
{
System.out.println(“Garbage collected from object” + i);
i++;
}
public static void main(String args[]){
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
//No Object Is Eligible for GC
t1.t=t2; //No Object Is Eligible for GC
t2.t=t3; //No Object Is Eligible for GC
t3.t=t1; //No Object Is Eligible for GC
t1=null;
//No Object Is Eligible for GC (Coz t2 still have a reference)
t2=null;
//No Object Is Eligible for GC (Coz t3 still hava a reference)
t3=null;
//All the 3 Object Is Eligible for GC (None of them have a reference)
System.gc();
}
}
/************************************/
output:
Garbage collected from object1
Garbage collected from object2
Garbage collected from object3
……………………………..
So, from the output we can clearly see that when JVMs run its garbage collector, garbage is collected from the 3 objects(t1, t2, t3 : as they dont hav any external refrence) and finalize method is called thrice.