Java – Garbage Collection

The runtime data area is a heap region that the JVM produces when it first starts up. All of the objects (class instance instances) are kept in this location. Due to space constraints, it is necessary to effectively manage this space by deleting any items that are no longer needed. Garbage collection, a component of Java memory management, is the act of eliminating useless objects from heap memory.

Automatic garbage collection is not supported by languages like C/C++, however it is automatic in Java.

We now know that Java’s trash collection is automated. Let’s examine the timing of Java’s garbage collection.

When does java perform garbage collection?

1. When the object is no longer reachable:

BeginnersBook obj = new BeginnersBook();  
obj = null;

Since we gave the reference obj a null value, it no longer points to the BeginnersBook object, making it inaccessible and useless. In this case, the reference obj was directing to the object of class BeginnersBook. In Java, trash collection is automatically provided for these objects.

Another example is

char[] sayhello = { 'h', 'e', 'l', 'l', 'o'};
String str = new String(sayhello);
str = null

Since we set the null value to str, the object “hello” that was previously existing in the heap memory is now useless. Here, the reference str of the String class was referring to a string “hello” in the heap memory.

2. When one reference is copied to another reference:

BeginnersBook obj1 = new BeginnersBook();
BeginnersBook obj2 = new BeginnersBook();
obj2 = obj1;

The instance (object) referred by (referenced by) obj2 in this case is not accessible and available for garbage collection since we have given the reference obj1 to obj2.

How to request JVM for garbage collection

We now understand that the inaccessible and useless items are available for trash collection, but the procedure doesn’t happen immediately. This implies that after the objects are prepared for garbage collection, they must wait for the JVM to execute the memory cleaning program that handles garbage collection. However, you may invoke the System.gc() function to request garbage collection from the JVM (see the example below).

Garbage Collection Example in Java

By invoking System.gc in this example, we are displaying garbage collection (). We have replaced a finalize() function in this code. Just before an object is destroyed by Java’s trash collection process, this method is called. This is the cause of the two invocations of this method you see in the output.

public class JavaExample{   
   public static void main(String args[]){  
        /* Here we are intentionally assigning a null 
         * value to a reference so that the object becomes
         * non reachable
	JavaExample obj=new JavaExample();  
        /* Here we are intentionally assigning reference a 
         * to the another reference b to make the object referenced
         * by b unusable.
	JavaExample a = new JavaExample();
	JavaExample b = new JavaExample();
	b = a;
   protected void finalize() throws Throwable
        System.out.println("Garbage collection is performed by JVM");


Garbage collection is performed by JVM
Garbage collection is performed by JVM





Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button