Java Garbage Collection And its Working With Program Example

Share

Introduction:

Terms:

Reachable & unreachable objects: Objects that can be accessed by a reference variable are known as reachable object. When there is no reference available to access an object, in any way,  it is called unreachable.

Garbage: Useless objects or unreachable objects or objects that can no longer be referenced.

Garbage collector: A program that takes care of memory and manages it automatically.

Garbage collection: It is a process of reclaiming already occupied memory referenced by unreachable objects.

finalization: A process of releasing resources held by the unreachable object and invoked just before destruction.

Journey of an Object

1. We use new keyword to create an object  and its reference gets stored in a reference variable of same class type.

2. Once the object finishes all its assigned tasks, it is highly recommended to make that object eligible for gc. So, that memory can be freed and used for further activities.

3. After making it eligible for gc, it is very important to release all the resources that are held by that object. For this intent, the finalize method is invoked.

4. And lastly, gc destroys that object and release the memory occupied by that object.

What is the need of Garbage Collector in Java ?

In C++, objects that are allocated using new operator should be destroyed manually using delete operator.

So, it is the programmer’s responsibility to remove that object from the heap memory. However, if one shows carelessness to do this job, the memory would become filled with unreachable objects.

As a result, a situation would arrive, where no further allocation of an object could be made and the most common error called ‘out of memory’ would be triggered.

But unlike C++, Java follows a different approach, it provides garbage collector whose purpose is to reclaim memory occupied by objects.

Therefore, there is no need to handle this situation manually as Garbage collector keeps monitoring memory for useless objects.

Garbage collector always run in the background to fulfill the need for memory management. It can’t be anticipated when would GC actually perform the cleaning task of memory as it depends on the approach that JVM follows.

However, it is certain that if a program is running in a low memory then JVM definitely going to execute GC.

How Does Garbage Collection in Java Work?

Unlike C++, the programmer doesn’t require to remove unused objects from heap memory in Java. Java provides a garbage collector that automates this process and its implementation lives in JVM.

Java garbage collection is a mechanism of identifying reachable & unreachable objects in heap memory, and cleaning unused ones for future allocation purpose. It’s the garbage collector’s responsibility to manage and reclaim the objects which can no longer be referenced.

What is GC root?

GC roots are the special objects to which all other objects are referenced from it and considered alive.

The garbage collector starts its process from the GC root and tracks all the objects that are directly or indirectly accessible through it on the heap. If an object can’t be referenced through GC root, then GC considers that object as a valid candidate for garbage collection

Garbage Collection Stages in Java

1) First, Garbage collector traverses each and every accessible object in the memory array through GC root. Objects that are accessible, considered as alive and inaccessible objects are considered as dead.

2) The marked dead objects occupy memory in the heap, which is then reclaimed by the Garbage collector for future object allocation.

When an object doesn’t have a reference variable, then it automatically becomes eligible for garbage collection.

Methods to run GC explicitly a brief Article on Method to run GC explicitly ]

Using system class

System class supports a static method called gc() to run the garbage collector. gc() method does not essentially run the Garbage collector, indeed, it requests JVM to execute it. Execution of gc totally depends on the approach that JVM follows.

By using runtime class

Runtime class also supports gc() method to make a call by requesting JVM to invoke gc. It is a singleton class that consists only a single object.

As a result, new instance can’t be created, instead, we use a factory method to get the reference of runtime class.

Before destroying an object, GC invokes finalize method in order to resource deallocation and perform cleaning operation.

Finalize method [ a brief Article on Finalize() ]

It allows us to perform some action before destroying a particular object.

There may be a situation where an object holds system resources and it is needed to be freed before the object gets destroyed. This is said to be finalization.

This method invokes beforehand reclaiming an object.

protected void finalize() { 
// action 
}

 

This post was last modified on July 14, 2022

Sandeep Verma

Published by
Sandeep Verma
Tags: finalization finalize method garbage collection garbage collector Garbage Collector in Java java Java concept object eligible for Garbage collection purpose of Garbage Collector runtime class system class unreachable object