Destructor

What is a Destructor?

  • A destructor is a special method that gets called automatically as soon as the life-cycle of an object is finished. 
  • A destructor is called to de-allocate and free memory. 
  • Syntax:

Class Object
{
protected void finalize()
{
//statements like closure of database connection
}
}
  • The following tasks get executed when a destructor is called.
    • Releasing the release locks
    • Closing all the database connections or files
    • Releasing all the network resources
    • Other Housekeeping tasks
    • Recovering the heap space allocated during the lifetime of an object
  • The destructor has a finalize() method in java.
  • The allocation and release of memory are implicitly handled by the garbage collector in Java.
  • How it works?
    • When the objects are created they are stored in the heap memory. 
    • These are accessible by main or child threads. 
    • So when these objects are no more used by the main thread or its child threads they become eligible for garbage collection and the memory which was acquired now becomes available by new objects being created. 
    • Before an object is garbage collected by the garbage collector the JRE (Java Runtime Environment) calls the finalize() method to close the input-output streams, the database connections, network connections, etc. 
Note that the finalize method called is protected. 
  • Why finalize is protected because it can be either called by the base class or derived class? 
    • Finalize method is present in the Object class. 
    • Thus in case you want to call this finalize method from other objects you can change this protected to public.
Properties of a destructor:
  • Overloading or inheritance is not allowed
  • No specification of access modifiers or parameters.
  • Automatic invocation and no explicit call from the user.
  • Used in classes but not in structures.
  • The order of the class varies from the most derived class to the least derived class
Advantages of Destructor in Java
  • The destructor destroys the value created by the constructor to space in heap memory.
  • Destructor is always called at the end of the program.
  • Destructor is never overloaded destructor doesn’t take any argument.
  • No need to define our constructor, the compiler creates for us one.
Garbage Collector
  • A garbage collector is a program that runs on the Java virtual machine to recover the memory by deleting the objects which are no longer in use or have finished their life-cycle. 
  • An object is said to be eligible for garbage collection if and only if the object is unreachable.
  • Let’s try to understand how garbage collection works in Java:
    • Garbage collection is mainly the process of marking or identifying the unreachable objects and deleting them to free the memory. 
    • The implementation lives in the JVM, the only requirement is that it should meet the JVM specifications. 
    • These are the different types of garbage collectors in Java:
      • Serial Garbage Collector
      • Parallel/Throughput Garbage Collector
      • CMS Collector
      • G1 Collector
  • Advantages of garbage collection in Java:
    • It automatically deletes the unused objects that are unreachable to free up the memory
    • Garbage collection makes Java memory efficient
    • It need not be explicitly called since the implementation lives in the JVM
    • Garbage collection has become an important and standard component of many programming languages

Java Finalize() Method
  • Therefore in case, there is a need for calling the destructor, it can be done with the help of the finalize method.
  • It is difficult for any developer to force the execution of a garbage collector.
  • But We can use the object.finalize() method which works exactly like a destructor in Java.
  • Syntax:
protected void finalize throws Throwable()
{
//Keep some resource closing operations here
}
  • An Object.finalize() method is inherited in all Java objects. 
  • finalize() method is protected as defined in java.lang.Object class.
  • finalize() method is called only once.
  • to override the finalize() method, you need to call the finalize method explicitly.
  • GC() is a service of JVM to execute Garbage Collection, it is called when the heap memory is full and needs memory for new arriving objects.
  • JVM ignores all exceptions except the unchecked exceptions that occur in the finalize method.
  • Full Example:
public class Demo
{
public static void main(String[] args)
{
Integer i = new Integer(2);
i = null;
System.gc();
System.out.println("In the Main Method");
}
protected void finalize()
{
System.out.println("object is garbage collected ");
}
}

Constructor Vs Destructor
  • A constructor is used to initialize an instance of a class
  • A destructor is used to delete or destroy the objects when they are no longer in use.
  • Constructors are called when an instance of a class is created.
  • Destructors are called when an object is destroyed or released.
  • Memory allocation
  • Releases the memory
  • Overloading is possible
  • Overloading is not allowed
  • They are allowed to have arguments
  • No arguments can be passed in a destructor


Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form