Java Garbage Collection

When we run java application, JVM will starts up and creates a heap area for storing objects. Since heap area is limited, It is required to manage heap area efficiently by removing the unreached objects. The process of removing unreached objects from heap memory is known as Garbage collection.

An object is said to be unreachable if it doesn’t contain any reference from the program.

Garbage collection in Java happens automatically during the lifetime of the program.

Garbage collector is the best example of Daemon thread as it is always running in background.

Ways to make an object eligible for Garbage collection

Even though programmer is not responsible to destroy objects, It is highly recommended to make an object eligible for Garbage Collection, If it is no longer required. An object is eligible for Garbage Collection if it doesn’t contain any references. There are five ways to make an object eligible for garbage collection.

  1. Nullifying the reference variable
  2. Re-assigning the reference variable
  3. Object created inside method
  4. Anonymous object
  5. Island of Isolation

Nullifying the reference variable

If an object is no longer required then assign null to all its reference variables then that object is automatically eligible for garbage collection.

Re-assigning the reference variable

If an object is no longer required then reassign its reference variable to some other object then the old object eligible for garbage collection.

Object created inside method

The objects created inside a method are by default eligible for garbage collection once method completes its execution.

Anonymous object

If we create an object without any reference variable then it becomes unreachable.

Island of Isolation

Island of isolation is a group of objects that reference each other but they are not referenced by any active object in the application.

Ways for requesting JVM to run Garbage Collector

Whenever we made an object eligible for GC, it may not be destroyed immediately by the garbage collector. Whenever JVM runs the GC then only unreached object will be destroyed. But we can’t expect exactly when JVM runs GC.

Instead of waiting until JVM runs GC, we can request JVM to run GC programmatically.

  1. System.gc() 
  2. Runtime.getRuntime().gc()

There is no guarantee that any one of above two methods will definitely run Garbage Collector.

System.gc() 

System class contain static method gc() for requesting JVM to run Garbage Collector.

Runtime.getRuntime().gc()

Runtime class allows the application to interact with the JVM. We can use gc() method to request JVM to run Garbage Collector.

It is recommended  to use System.gc() over Runtime.gc()

Finalization

The finalize() method is a special method which is called by the garbage collector on an object which is eligible for Garbage Collection. Object is eligible for garbage collection when there are no more references to the object(unreferenced object). Once the finalize() method completes, immediately Garbage Collector destroy that object. finalize() method is defined in java.lang.Object class and its syntax is as follows.

finalize() method is used to perform clean-up activity. Clean-up activity could be resource releasing(DB Connection, Network Connection, File, stream … etc.).

Since Object class contains the finalize() method it inherits into every java object. So Garbage Collector calls finalize() method on all java objects.

The finalize method in the Object class, has an empty implementation. If we have clean-up activities in our class then we have to override this method to define our own clean-up activities.

Example 01 : finalize() method called by Garbage Collector

Output :

finalize() method

In the above example, we are calling System.gc(); for requesting JVM to invoke Garbage Collector. But there is no guarantee that always garbage collector executes with System.gc().

Example 02 : Explicitly invoke finalize() method

Output :

main begin
finalize() method
main end
finalize() method

In the above example, As we are invoking finalize() method explicitly, object won’t get destroyed. To prove that, after calling finalize() method explicitly we are calling toString(); method, it executed without fail. It proves that when we invoke finalize() method explicitly that method will be executed like a normal method.

Example 03 : Explicitly invoke finalize() method – exception raised in finalize() execution

Output :

main begin
finalize() method begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize.Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.finalize(Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.java:9)
at com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize.Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.main(Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.java:20)

In the above example, We are calling finalize() method explicitly and we can see that program terminated as there is exception raised in finalize() method execution.

Exception could be checked or unchecked program gets terminated when we invoke finalize() method explicitly.

Example 04 : finalize() method called by GC – exception raised in finalize() execution

Output :

main begin
main end
finalize() method begin

In the above example, Garbage Collector calling finalize() method. We can see that program not terminated even exception raised in finalize() method execution. But note that, after exception raised remaining lines of finalize() method are ignored and even exception is not displayed in the console.

Exception could be checked or unchecked remaining lines of finalize() method are ignored and exception details will not be printed on the console when finalize() method invoked by Garbage Collector.

Scroll to Top