- By making a reference variable pointing to null.
class A
{
public static void main(String... ar)
{
A ob = new A();
ob=null; //making a reference variable pointing to null.
}
}
As you may see in the diagram below that we have made a reference ob point to null, doing so makes this object(small blue circle) on heap(the big red circle) not
referenced by any reference variable(hence the dotted line), which makes
this object eligible for garbage collection.
- By reassigning a reference variable to another object.
class A
{
public static void main(String... ar)
{
A ob1 = new A(); //first object of A
A ob2 = new A(); //second object of A
ob1=ob2; //making ob1 refer to the same object referred by ob2.
}
}
- We have made two objects of class A, with each object referenced by a separate reference variable.
- Next, we made the first reference variable ob1 point to the second object(also referenced by ob2).
- Doing this, made the first object which was earlier referenced by ob1 eligible for garbage collection because no reference variable
is pointing to it anymore.
Please refer to the diagram below for ease of understanding.
How do you make the garbage collector run?
Garbage collector is in the full control of Java Virtual Machine(JVM). Hence, only JVM decides when the garbage collector should run. We cannot force JVM to run the garbage collector but we can
only request JVM in two ways-
- By calling a static method of System class -System.gc();
- By calling a method of Runtime class - gc();
Advertisement
What happens by calling a gc() method of System or Runtime class?
Even after calling gc() method of either System or Runtime class, JVM may or may not run the garbage collector. So please, don't ever rely on JVM to run
the garbage collector after you've called gc() method. Remember, calling gc() method is just making a request to JVM to run the garbage collector. To oblige
to the request is a sole wish of JVM. Sometimes, JVM may run the garbage collector after you have gc() method, while on other occasion, it just won't.
Garbage collection example
Let's understand how we make a request to JVM by an example and let's see if it runs -
class A
{
public static void main(String... ar)
{
Runtime run = Runtime.getRuntime();
System.out.println("Free memory at the beginning of program - " +run.freeMemory());
A ob;
for(int i=0; i<10000;i++)
{
ob= new A();
}
System.out.println("Free memory after creating 10000 objects- " +run.freeMemory());
run.gc(); //requesting the garbage collector to run.
System.out.println("Free memory after running garbage collector - " +run.freeMemory());
}
}
Output-
Free memory at the beginning of program - 95462328
Free memory after creating 10000 objects- 94880608
Free memory after running garbage collector - 95649216
Program Analysis
- In the preceding code, free memory in JVM is calculated at the beginning of the program.
- Next in for-loop, we have created 10000 objects of A class and the free memory in JVM is re-calculated.
- When the for-loop ran for the last time, only then the last object is referenced by reference variable ob.
- Rest 9999 objects aren't referenced by any reference variable and they are good for garbage collection.
- Surprisingly on the execution of run.gc() method, garbage collector runs and it has freed the memory, which is proven by calling freeMemory() method of Runtime class.
Note :
On many occasions garbage collection might not run at all at your request, so please don't rely on its execution.
Please share this article -
Advertisement