Java Garbage Collection

Hello everyone!!! Welcome to flower brackets blog. Today in this post you will learn java garbage collection.

First let us understand what exactly java garbage collector is, in Java??

Java Garbage Collection

What is java garbage collection?

Java Garbage Collection is the process of deleting unused objects in heap memory.

Garbage collection in java is automatic. In this process compiler identifies the objects which are in use and which are not and deletes the unreferenced objects that is no longer referenced by any part of program.

The java program allows you to create as many objects you want. You do not have to worry about destroying them.

The java runtime environment deletes objects when it determines that they are no longer being used. This process is called Garbage Collection.

Also Read – String Concatenation Java

An object is eligible for garbage collection when there are no more references to that object.

References that are held in a variable are usually dropped when the variable goes out of scope.

Or you can explicitly drop an object reference by setting the variable to special value “null”.

Remember that program can have multiple references to the same object.

All references to an object must be dropped before the object is eligible for garbage collection java.

Some object oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they no longer needed.

Managing memory explicitly is tedious and error-prone.


Finalize method

Finalize method in java is called each time before the object is garbage collected. This method can be used to perform cleanup processing. This method is defined in object class like this,

protected void finalize
{
   
}

Inside this finalize method we can write cleanup related codes. When garbage collection determines that the particular object does not have any reference then this method will be called by the garbage collector.

Garbage collection is performed by a daemon thread called Garbage Collector(GC).

The Garbage Collector calls the finalize method before object is garbage collected.

Also Read – Sort String Array Java

JVM garbage collection collects only those objects that are created by new keyword.

If we have created any object without new keyword, we can use finalize method to perform cleanup processing.


GC() method

Here you can see gc() method,

public static void gc()
{
   
}

Using this gc() method we can invoke garbage collector to perform cleanup processing.

We can find this gc() method in System and Runtime classes.

Note :

Neither finalization nor garbage collection is guaranteed.

Example :

public class Employee
{
   String name;
   int age;
 
   public Employee(String name, int age)
   {
      this.name = name;
      this.age = age;
   }
 
   public void finalize()
   {
      System.out.println(this + " object is garbage collected," + " name : " + this.name);
   }
}

public class GarbageCollector
{
   public static void main(String[] args) throws InterruptedException
   {
      Employee objVariable1 = new Employee("Sam", 14); // reference variable
      Employee objVariable2 = new Employee("Ben", 23); // reference variable
 
      objVariable1 = null; // now objVariable1 is available for garbage collection
      objVariable2 = null; // now objVariable2 is available for garbage collection
 
      System.gc();
 
      Thread.sleep(500);
   }
}

Output:

Employee@11c8b3c object is garbage collected, name : Ben
Employee@1db75af object is garbage collected, name : Sam


Unreferencing an object

There are three ways we can unreference an object. They are,

  1. By nulling the reference
Employee objVariable1 = new Employee("Sam", 14);
objVariable1 = null;

Here I have created a new Employee object. And this Employee object is referred by “objVariable1”.

Then I am assigning “null” to “objVariable1” so that this Employee object is eligible for garbage collection.

2. By assigning a reference to another reference

Employee obj1 = new Employee("Sam", 14);
Employee obj2 = new Employee("Ben", 23);
obj1 = obj2;

Here I have created Employee object and this Employee object is referred by “obj1”.

I have created another Employee object this Employee object is referred by “obj2”.

Also Read – Preface To Java Virtual Machine And Architecture

And finally I’m assigning “obj2” to “obj1” so both “obj1” and “obj2” points to second Employee object.

So this first Employee object is eligible for garbage collection.

3. By anonymous object

new Employee();

This is how we can create anonymous object and this object does not have any reference so this object is eligible for garbage collection.

Example:

public class Employee
{
   String name;
   int age;
 
   public Employee(String name, int age)
   {
      this.name = name;
      this.age = age;
   }
}

public class GarbageCollector
{
   public static void main(String[] args)
   {
      Employee objVariable1 = new Employee("Sam", 14);
      objVariable1 = null; // now object referred is available for garbage collection
 
      Employee obj1 = new Employee("Sam", 14);
      Employee obj2 = new Employee("Ben", 23);
      obj1 = obj2; // now obj1 is available for garbage collection
 
      new Employee("Eric", 19); // Anonymous Employee object
   }
}


Advantages

Here are advantages of garbage collection in java,

  1. Java garbage collection makes memory efficient because garbage collector removes unreferenced objects from heap memory.
  2. Garbage collection in java is automatically done by java garbage collector which is a part of JVM. Hence we don’t need extra efforts.

Garbage collection example

 

import java.util.*;

public class GarbageCollection
{
   public static void main(String[] args)
   {
      Runtime rt = Runtime.getRuntime();
      System.out.println("Before garbage collection: Available free memory in JVM = " + rt.freeMemory());
      rt.gc();
      System.out.println("After garbage collection: Available free memory in JVM = " + rt.freeMemory());
   }
}

Here I have not shown the output since the numbers vary from one system to another. Obviously numbers are not important.

The fact we have to consider here in this example is memory available is more than before.


conclusion

That’s it guys. This was all about how garbage collection works in java. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like