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 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.
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
- package demo;
- import java.util.*;
- public class Demo
- {
- public static void main(String[] args) throws InterruptedException
- {
- Runtime rt = Runtime.getRuntime();
- long m1, m2;
- String[] anyints = new String[10000];
- System.out.println("TOTAL MEMORY : " + rt.totalMemory());
- m1 = rt.freeMemory();
- System.out.println("INITIAL FREE MEMORY : " + m1);
- rt.gc();
- m1 = rt.freeMemory();
- System.out.println("AFTER GARBAGE COLLECTION : " + m1);
- for (int i = 0; i < 10000; i++)
- {
- anyints[i] = i+"";
- }
- m2 = rt.freeMemory();
- System.out.println("AFTER ALLOCATION : " + m2);
- System.out.println("MEMORY USED BY ALLOCATION : " + (m1 - m2));
- for (int i = 0; i < 10000; i++)
- {
- anyints[i] = null;
- }
- rt.gc();
- Thread.sleep(100);
- m2 = rt.freeMemory();
- System.out.println("FREE MEMORY AFTER COLLECTING DISCARDED STRINGS : " + m2);
- }
- }
Output
Summary
This article explained the garbage collection in Java.