Garbage collection and finalize method3 min read

In this tutorial, we are going to cover garbage collection and finalize method in Java.

Garbage collection

Garbage collection is a method through which Java programs do automatic memory management.

  • Java programs are compiled into byte-code that may be executed by a Java Virtual Machine, or JVM. Objects are produced on the heap, which is a part of memory devoted to the Java application, while it runs on the JVM.
  • Some objects will become obsolete over time. To free up memory, the garbage collector detects these useless objects and deletes them.
  • As needed, Java applications obtain objects in memory. Garbage collection (GC) in the Java virtual machine (JVM) is responsible for automatically determining what memory is no longer in use by a Java application and recycling it for other purposes.
  • Because the JVM automatically reclaims memory, Java application developers don’t have to worry about explicitly freeing memory objects that aren’t in use.
  • The garbage collection method is based on the assumption that most objects used in Java programming are temporary and can be recovered soon after they are created.
  • In Java, it is the responsibility of the system, not the programmer, to keep track of which objects are “garbage”. The object doesn’t become garbage until all the references have been dropped.
  • As Java uses garbage collection, errors like a memory leak or dangling pointer are simply impossible. Because of Java’s automatic garbage collection, a leak of cleanup is done automatically.

finalize() method

  • When an object is destroyed, it may be necessary for it to do some action. If an object holds a non-java resource, such as a filehandle or a window character font, you should ensure that these resources are removed before the object is destroyed.
  • Java has a mechanism called finalization that can be used to deal with such scenarios. You can define particular actions that will occur when an item is about to be reclaimed by the trash collector by utilizing finalization.
  • Before an object is deleted from memory, this function is used to conduct certain final actions on it. You can override the finalize() method to keep any operations running before the object is destroyed.
  • In C++, we use destructor methods, but in Java, this situation is handled by a special mechanism called finalization. Using this mechanism we can define a specific action that will occur when an object is just about to be reclaimed by the garbage collector.
  • You just define the finalize() method to add a finalize to a class. When the Java runtime is going to recycle an object of that class, it calls that method. You will specify the actions that must be completed before an object is destroyed in the finalize() method.
  • The trash collector runs on a regular basis, looking for items that are no longer referenced directly or indirectly by other objects. The java run time runs the object’s finalize() method just before it is freed.

The general form of finalize method is:

protected void finalize(){
  // code
}

Example:

public class FinalizeDemo{
  FinalizeDemo(){
    System.out.println("This is constructor");
  }
  
  @Override
  protected void finalize(){
    System.out.println("This is finalize() method");
  }
  
  public static void main(String[] args){
    FinalizeDemo finalizeDemo = new FinalizeDemo();
    System.out.println(finalizeDemo.hashCode());   
    finalizeDemo = null; 
    System.gc();   
    System.out.println("garbage collection end");
  }
}

Output:

This is constructor
705927765
garbage collection end
This is finalize() method

Reading recommendation

Loops in Java


Share:

Leave a Reply