Last Updated : 23 Jul, 2025
Prerequisite -
Garbage Collection in Java Difficulty level :Intermediate In Java, object destruction is taken care by the
Garbage Collectormodule and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage collection. Predict the output of following Java Programs:
public class Test
{
public static void main(String[] args) throws InterruptedException
{
String str = new String("GeeksForGeeks");
// making str eligible for gc
str = null;
// calling garbage collector
System.gc();
// waiting for gc to complete
Thread.sleep(1000);
System.out.println("end of main");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
}
}
Output:
end of mainExplanation : We know that finalize() method is called by Garbage Collector on an object before destroying it. But here, the trick is that the str is String class object, not the Test class. Therefore, finalize() method of String class(if overridden in String class) is called on str. If a class doesn't override finalize method, then by default Object class finalize() method is called.
public class Test
{
public static void main(String[] args) throws InterruptedException
{
Test t = new Test();
// making t eligible for garbage collection
t = null;
// calling garbage collector
System.gc();
// waiting for gc to complete
Thread.sleep(1000);
System.out.println("end main");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
System.out.println(10/0);
}
}
Output:
finalize method called end mainExplanation : When Garbage Collector calls finalize() method on an object, it ignores all the exceptions raised in the method and program will terminate normally.
public class Test
{
static Test t ;
static int count =0;
public static void main(String[] args) throws InterruptedException
{
Test t1 = new Test();
// making t1 eligible for garbage collection
t1 = null; // line 12
// calling garbage collector
System.gc(); // line 15
// waiting for gc to complete
Thread.sleep(1000);
// making t eligible for garbage collection,
t = null; // line 21
// calling garbage collector
System.gc(); // line 24
// waiting for gc to complete
Thread.sleep(1000);
System.out.println("finalize method called "+count+" times");
}
@Override
protected void finalize()
{
count++;
t = this; // line 38
}
}
Output:
finalize method called 1 timesExplanation : After execution of line 12, t1 becomes eligible for garbage collection. So when we call garbage collector at line 15, Garbage Collector will call finalize() method on t1 before destroying it. But in finalize method, in line 38, we are again referencing the same object by t, so after execution of line 38,this object is no longer eligible for garbage collection. Hence, Garbage Collector will not destroy the object. Now again in line 21, we are making same object eligible for garbage collection one more time. Here, we have to clear about one fact about Garbage Collector i.e. it will call finalize() method on a particular object exactly one time. Since on this object, finalize() method is already called, so now Garbage Collector will destroy it without calling finalize() method again.
public class Test
{
public static void main(String[] args)
{
// How many objects are eligible for
// garbage collection after this line?
m1(); // Line 5
}
static void m1()
{
Test t1 = new Test();
Test t2 = new Test();
}
}
Question : How many objects are eligible for garbage collection after execution of line 5 ? Answer :
2Explanation : Since t1 and t2 are local objects of m1() method, so they become eligible for garbage collection after complete execution of method unless any of them is returned.
public class Test
{
public static void main(String [] args)
{
Test t1 = new Test();
Test t2 = m1(t1); // line 6
Test t3 = new Test();
t2 = t3; // line 8
}
static Test m1(Test temp)
{
temp = new Test();
return temp;
}
}
Question : How many objects are eligible for garbage collection after execution of line 8? Answer :
1Explanation : By the time line 8 has executed, the only object without a reference is the one generated i.e as a result of line 6. Remember that "Java is strictly pass by value" so the reference variable t1 is not affected by the m1() method. We can check it using finalize() method. The statement "System.out.println(this.hashcode())" in finalize() method print the object hashcode value on which finalize() method is called,and then just compare the value with other objects hashcode values created in main method.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4