Automatic Garbage Collection in Java


Java don't want you to get stuck with pointers and leave so many dangling pointer refrences, so it manages the memory and remove the pointer out of your way. this article highlight the key process of garbage collection in java.

As the title suggest the Garbage collection in Java is automatic. JVM takes off all the load to manage the memory and the pointers in Java.

But, Java provides you a manual option for the Garbage collection too. Let us see the Garbage Collection mechanism in java first.

Heap: The runtime heap is where all the objects and references are stored. So, obviously this is where we need the garbage collection.

As the title suggest the Garbage collection in Java is automatic. JVM takes off all the load to manage the memory and the pointers in Java.

But, Java provides you a manual option for the Garbage collection too. Let us see the Garbage Collection mechanism in java first.

Heap: The runtime heap is where all the objects and references are stored. So, obviously this is where we need the garbage collection.

Heap Structure

This whole process occurs in three steps:

Step 1: Marking: The JVM's GC identifies and marks the free space and non required objects or variables.Marking

Step 2: Deletion: The JVM then deletes all these marked elements and the space is free for reuse, but to further improve the performance JVM takes another step.

Normal Deletion

Step 3: Compacting: Finally JVM moves the unallocated part to the end of heap where the free space is placed and updates the pointer for the existing references.

Deletion with compacting

The Heap is actually managed by GC in different way than other languages. JVM allocate the heap according to the life time of the fields in it. This is divided in following sections.

Young Generation: the fields or variables with the shortest lifetime. The garbage collection in this part is known as minor garbage collection and while doing this all the other executions are stopped.

Older Generation: here the variables are allocated which may have a longer life time. The Collection in this area is known as major garbage collection and this shall be minimised as much as possible to improve the responsiveness of program.

Permanent Generation: This part is allocated to the part which will keep alive throughout the program's life time, that part is known as the meta data this consist of information about the classes and methods which are constantly used by JVM for program execution.

There is nothing much to do in the GC in java, as the above discussion tells that the GC is automated process by the JVM. However if you want to ask the JVM to run this process you can do so using the method,

System.gc();

or

Runtime.getRuntime().gc(); //this is equivalent to the above one.

Heap Structure

This whole process occurs in three steps:

Step 1: Marking: The JVM's GC identifies and marks the free space and non required objects or variables.Marking

Step 2: Deletion: The JVM then deletes all these marked elements and the space is free for reuse, but to further improve the performance JVM takes another step.

Normal Deletion

Step 3: Compacting: Finally JVM moves the unallocated part to the end of heap where the free space is placed and updates the pointer for the existing references.

Deletion with compacting

The Heap is actually managed by GC in different way than other languages. JVM allocate the heap according to the life time of the fields in it. This is divided in following sections.

Young Generation: the fields or variables with the shortest lifetime. The garbage collection in this part is known as minor garbage collection and while doing this all the other executions are stopped.

Older Generation: here the variables are allocated which may have a longer life time. The Collection in this area is known as major garbage collection and this shall be minimised as much as possible to improve the responsiveness of program.

Permanent Generation: This part is allocated to the part which will keep alive throughout the program's life time, that part is known as the meta data this consist of information about the classes and methods which are constantly used by JVM for program execution.

There is nothing much to do in the GC in java, as the above discussion tells that the GC is automated process by the JVM. However if you want to ask the JVM to run this process you can do so using the method,

System.gc();

or

Runtime.getRuntime().gc(); //this is equivalent to the above one.

NOTE: if you are an advance level researcher or you like to dive deeply into concepts you can find more details on the links given in credit section.

Reference:
This article is prepared with reference of Official Java docs