Java - Class - Garbage Collection and finalize method

Java

In this tutorial we will learn about garbage collection and finalize method in Java programming language.

What is Garbage Collection in Java?

Garbage Collection is a technique that automatically deallocates memory space assigned to an object when no reference to the object exists.

In programming language like C++ we have to manually deallocate memory space given to an object but not in Java.

So, when we instantiate an object using the new keyword a certain size of memory is dynamically allocated to the object and the reference of that memory location is saved in the object variable.

For example, if we have a class Awesome and we instantiate an object person then a memory space will be allocated for the newly created object and the reference of that memory space will be saved in the object.

Awesome person = new Awesome();

Now, if we set the person object value to null then the reference of the allocated memory space will be lost.

person = null;

Java checks for such dynamically allocated memory space that are no longer being referenced and it assumes that they are no longer needed and so it reclaims the allocated space.

Garbage Collection in Java occurs irregularly during our program execution and Java run time implementation have varying approach to garbage collection. So, it is not guaranteed that the garbage collection will occur at the time of our code execution. But we don't have to worry about freeing memory spaces at the time of writing our code because it will be handled by Java in the end.

The finalize method

Before an object is destroyed we might want to perform some final tasks like freeing non-Java resources like file handle. And to perform such takes we create the finalize method in the class which gets executed before the object is destroyed.

Syntax

protected void finalize() {
  // some code ...
}

Java run time calls the finalize method of the class before the object is recycled.

Example

In the following example we are creating an Awesome class having constructor, a method and the finalize method.

class Awesome {

  // member variable
  int awesomeness;
  
  // constructor
  Awesome(int awesomeness) {
    System.out.println("Inside the Awesome class constructor.");
    this.awesomeness = awesomeness;
    System.out.println("Awesomeness set!");
  }

  // method
  public int getAwesomeness() {
    return this.awesomeness;
  }

  // finalize
  protected void finalize() {
    System.out.println("Inside the Awesome class finalize method...");
    System.out.println("Object getting destroyed... Bye...");
  }
}

class AwesomeExample {
  public static void main(String[] args) {
    // create an awesome object
    Awesome obj = new Awesome(10);

    // now print the awesomeness
    System.out.println("Awesomeness of the object is " + obj.getAwesomeness());
  }
}

Output:

$ javac AwesomeExample.java 
$ java AwesomeExample
Inside the Awesome class constructor.
Awesomeness set!
Awesomeness of the object is 10

Note! In the above output the finalize method was not called by the Java run time meaning the garbage collection didn't occur. If it is called then we will see the message from the finalize method.