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
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
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:
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.
Heap Generation for Garbage Collection
Objects are created on the heap. The heap consists of the following three parts.
Example
Output
Summary
This article explained the garbage collection in Java.
Solutions Manual to Objects First with Java – A Practical Introduction using BlueJ