Introduction

Java has very strong memory management. In Java, when an object is not of some use, or we can say that we do not need that object in the future, then it destroys that specific object. The amount of memory is now free for any other use that was occupied previously. This entire process is done by the Garbage Collector in Java. Unlike many other programming languages, we do not need to start the Garbage Collector in Java. It starts automatically and does his work continuously. That is why it has advantages over other programming languages.

Garbage Collection in Java

  • In Java, when objects are created, they are stored on the heap.
     
  • The Garbage Collector in Java deletes those objects that are not needed now or in the future.
     
  • This will free up space, or we can say that there will be more free space for the creation of new objects in Java.
     
  • It automatically knows which objects are of use and which are not.
     
  • In Java, garbage collection is provided by the Java Virtual Machine.
     
  • We cannot force the Garbage Collector to run.
     
  • We can only request it to run but it is not certain that it runs when you say to run it.
     
  • It will run automatically.

Advantage of Garbage Collection in Java

Since the Garbage Collector works automatically, it reduces the load on the programmer, because it is very complex to know which object is of use and which is not. On the other hand in Java the Garbage Collector also allocates memory to the newly created objects.

Types of Garbage Collector

The following are the types of the Garbage Collector:

  • Throughput Garbage Collector
     
  • Concurrent Low Pause Controller
     
  • Incremental Low Pause Collector

Eligibility for Garbage Collection

A Garbage Collector deletes an object when it is not used, or we can say that when it does not have any references. This is the eligibility of an object to be garbage collected. In Java, cyclic dependencies are not considered as the reference. For example, if an object x has a reference for an object y and object y has a reference for object x then this is known as cyclic dependency. These objects are deleted by the Garbage Collector. In Java, when a parent object becomes null, its child object is automatically available for garbage collection.

Garbage Collection Roots

In Java, there are root objects. When the application reaches these roots the object is reachable. The garbage collection root is reachable in Java.


GC roots


Heap Generation for Garbage Collection

Objects are created on the heap. The heap consists of the following three parts.

  • Young generation
     
  • Tenured or Old generation
     
  • Perm Area of Heap

Example

  1. package demo;  
  2. import java.util.*;  
  3. public class Demo  
  4. {  
  5.     public static void main(String[] args) throws InterruptedException  
  6.     {   
  7.         Runtime rt = Runtime.getRuntime();   
  8.         long m1, m2;   
  9.         String[] anyints = new String[10000];   
  10.         System.out.println("TOTAL MEMORY : " + rt.totalMemory());   
  11.         m1 = rt.freeMemory();   
  12.         System.out.println("INITIAL FREE MEMORY : " + m1);   
  13.         rt.gc();   
  14.         m1 = rt.freeMemory();   
  15.         System.out.println("AFTER GARBAGE COLLECTION : " + m1);   
  16.         for (int i = 0; i < 10000; i++)   
  17.         {  
  18.             anyints[i] = i+"";  
  19.         }   
  20.         m2 = rt.freeMemory();   
  21.         System.out.println("AFTER ALLOCATION : " + m2);   
  22.         System.out.println("MEMORY USED BY ALLOCATION : " + (m1 - m2));   
  23.         for (int i = 0; i < 10000; i++)   
  24.         {  
  25.             anyints[i] = null;  
  26.         }   
  27.         rt.gc();  
  28.         Thread.sleep(100);   
  29.         m2 = rt.freeMemory();   
  30.         System.out.println("FREE MEMORY AFTER COLLECTING DISCARDED STRINGS : " + m2);   
  31.     }   
  32. }  

 

Output


garbage collection example

Summary

This article explained the garbage collection in Java.