Java Garbage Collection

Question – Explain garbage collection?
How you can force the garbage collection?
What is Garbage Collection and how to call it explicitly?

Garbage collection is one of the most important features of Java.
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use.
In Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is an automatic process and can’t be forced. There is no guarantee that Garbage collection will start immediately upon request of System.gc().

public class GarbageCollectionExample {
GarbageCollectionExample t;
	static int i = 1;
	protected void finalize() {
	System.out.println("Garbage collected from object" + i);
	i++;
	}
	public static void main(String args[]) {
		GarbageCollectionExample t1 = new GarbageCollectionExample();
		GarbageCollectionExample t2 = new GarbageCollectionExample();
		GarbageCollectionExample t3 = new GarbageCollectionExample();
		
		// 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.

Question – What kind of thread is the Garbage collector thread?

It is a daemon thread.

Question – Can an object’s finalize() method be invoked while it is reachable?

An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.

Question – Does garbage collection guarantee that a program will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

Question – What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup, before the object gets garbage collected. For example, closing an opened database Connection.

Question – If an object is garbage collected, can it become reachable again?

Once an object is garbage collected, It can no longer become reachable again.

Leave a Reply