Java

Garbage Collection in Java | Explained

Java provides a concept of Garbage Collection to make the memory more efficient. The term garbage means unreferenced objects while garbage collection refers to a process that reclaims the runtime unused memory automatically. The free(), and delete() functions are used in C language and C++ language respectively to achieve the garbage collection however in java, it is performed automatically and hence java provides better memory management.

This write-up presents the following aspects of Java garbage collection:

  1. What is Java Garbage Collection?
  2. Advantages of Java Garbage Collection
  3. How to Unreferenced Object
  4. finalize() method in Garbage Collection
  5. System.gc() method in Garbage Collection
  6. Java Garbage Collection Examples

So, let’s start!

What is Garbage Collection in Java

The destruction of an “out of used” object can be done with the help of garbage collection. No reference to an object means there is no need to keep that object, and hence memory occupied/taken by that object will be released. The phenomenon of automatically releasing the memory is called garbage collection in java.

Advantages of Java Garbage Collection

Java garbage collection provides multiple advantages some of them are listed below:

  • Automatic deletion of unnecessary/unused objects is one of the most significant benefits of Java Garbage Collection.
  • Java Garbage Collection makes memory efficient as it deletes the unreferenced objects from the memory automatically.

How to Unreferenced Object

The objects that are unreferenced (no part of the program is pointing to that object) can be reclaimed by garbage collection. There are three ways to unreferenced objects which are listed below:

Assign a Null Reference

The first way to unreferenced the objects is to assign the null reference to the object.

Student std = new Student();
std = null;

Now, std is available for garbage collection.

Assign one reference to other

Another way to achieve garbage collection is by assigning a reference to another. For example:

Student std1 = new Student();
Student std2 = new Student();
std1 = std2;

Here, std1 is eligible for the garbage collection.

Anonymous Objects

The third way to unreference the objects is the use of the anonymous object.

new Student();

Directly calling the constructor.

finalize() method in Garbage Collection

It is a method of the object class that can be invoked/called every time before the object is garbage collected and the purpose of finalize() method is to perform cleanup processing.

System.gc() method

A function that can be used to invoke/call the garbage collector in order to perform cleanup processing.

Java Garbage Collection Examples

Let’s consider a practical example for a profound understanding of the concepts:

Example

In this example, we will assign the null reference to the object to make it eligible for garbage collection.

class Student{
    Student(){
        System.out.println("Object Created");
    }
    protected void finalize(){
        System.out.println("Object destroyed");
    }
}
 
public class GarbageCollectionExample {
    public static void main(String[] args) {
        Student std = new Student();
        std = null;
        System.gc();
    }
}

We override the finalize method of the object class for cleanup processing then we utilize the System.gc() function that will request the JVM to call the Garbage collector and hence destroys the object:

The output authenticates the working of garbage collection.

Similarly, the other two ways can be used to unreference the objects i.e. anonymous object, and assign one reference to another.

Conclusion

In java, the garbage collector looks for the unnecessary/unused objects and removes them from the memory. Objects can be unreferenced in three different ways such as assigning null reference, assigning one reference to another, and using the anonymous object. finalize() method is invoked/called each time just before the object is garbage collected while System.gc() runs the garbage collector and can be invoked for cleanup processing. This write-up presents a detailed overview of java garbage collection.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.