Java Garbage Collection

java garbage collection

I was trying to find the best information on Java garbage collection and why java does not support destructors. Almost read 100 of blogs and articles on Java garbage collector and destructors method in java. I have found to the point answer but I was looking for the process and was looking for the exact method by which the JVM performs this task.
so today In this article I am going to share my knowledge to make understand others easily so that they can make sense of Java garbage collection. This blog will help you to understand what is going in the background when java garbage collection is being processed.
So before going to the process, I think you might know some several things I am going to describe below. But if you are well known to these things then you might skip these.

One of the major difference between c++ and java : 

Both are high level and object-oriented programming language but the major difference is Java does not have destructor element as c++ have. Instead of destructor java uses Garbage collector to clear off unused memory automatically.
We may have a lot of unused objects. But we humans are always in search of better memory management technique. To save up our memory we were using free() function in c language and delete() function in c++.
The main advantage of Java is here, In Java, this task is done automatically by the java which is known as Java Garbage Collector. So we can always say that Java provides us better memory management as we don’t have to make an extra effort on creating functions to delete unused or unreferenced objects.

so till now, I know you guys are known to the fact what is the purpose of the Java garbage collector and why java does not have a destructor (as Java has its own java garbage collection so no need to have destructor as c and c++ have).

But I am sorry to say that you need to know more in details otherwise you may have the wrong conception on this topic. ( like we can use a function to free memory in java too like c++ )
so I am going deeper to clear all the questions that might be appeared on your mind.

You may also learn,

Concept Of Class and Object in Java With Examples

What is Destructor:  

Objects are born with a life cycle. when an object’s life cycle is over, a special method is called in order to clear off the memory and de-allocate the resources. This method is known as a destructor. It is also called manual memory management. the destructor is used in order to avoid memory leaks.
The destructor is used by the programmer and there are several rules of using a destructor.
Rule 1. The class name and the destructor name must be same.
Rule 2. There must not be arguments.
Rule 3. The destructor must not have any return type.
So it is clear that we have to make an extra effort here to clean up the memory.

Concept Of garbage collection/collector :

As you saw on the above few lines we should manually implement destructor and it is done by the programmer But in case of java, there is a concept of garbage collection where JVM automatically performs the task of cleaning up the unused memory.
It is a program that actually runs on Java Virtual Machine. This program deletes the objects that are not used anymore in the future or delete the objects that are not accessible from the code anymore. (these types of objects are also sometimes called unreferenced objects ) . This runs automatically on JVM and checks periodically if there is an object which is unreferenced in the memory heap. If any unreferenced object is found in the memory heap that signifies that the object is totally useless and will never be used in future, so the garbage collector gets rid of the object and frees up the memory which was allocated by the unreferenced object.
So there are two main basic principles of garbage collection.
#1. To find unreferenced objects that can not be used in the future anymore
#2. To clean up the memory and reclaim the resources used by the object.

You might now be thinking that how Java finds the unreferenced objects?

It is very interesting to know that actually, java does not find unreferenced objects, java actually finds all the referenced objects in a program and the mark rest of the objects in that program as unreferenced objects. (cool way of finding unreferenced objects in a java program )

Those who are interested in Using Garbage Collection manually can use the below parameters to use different types of Garbage Collection.
1. The serial collector:


2. Parallel Collector:


There are more types we will discuss those in advance garbage collector in later.

How an object has become unreferenced?
#1. Nulling a reference

Student e=new Student();  

#2. Assigning a reference object to another

Student r1=new Student();  
Student r2=new Student();  
r1=r2;//now the first object referred by r1 is available for garbage collection  

We programmer can use finalize() method .  This method is used to clean up memory in java.
This method is used in the below format

protected void finalize(){}

We can create an object using new keyword and we can create an object without the new keyword too. But in JVM garbage collector only collect those objects which are created by using new keyword so In case if you have created an object without using the new keyword then you have to use this finalize() method to use clean up process manually.

Now I think you have enough knowledge of garbage collection in java. So now we can take a look at simple java garbage collection example by coding.

public class MyGarbage{  
 public void finalize(){
      System.out.println("Garbage collected successfully");
 public static void main(String args[]){  
  MyGarbage s1=new MyGarbage();  
  MyGarbage s2=new MyGarbage();  

the gc() method is also used to clean up the memory and it is used in system and runtime classes.

Garbage collected successfully
Garbage collected successfully

Reading this blog I hope you are sure that there are a lot of advantages of a Java garbage collector.
But there is another side of this coin.
like everything, this garbage collection too has some disadvantages.

disadvantages of garbage collection in Java

#1. Java Garbage Collector runs on its own thread still it can take effect on performance.
#2. Since Java Garbage Collector has to keep constant tracking on the objects which are not referenced It adds overhead.
#3. Java Garbage Collector needs some amount of resource to identify which memory needs to be freed and which not.
#4. It’s almost impossible to predict how much time can be taken to collect garbage by the JVM.

Hope you enjoyed learning.


Leave a Reply

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