3 - Destructor & Finalize
3 - Destructor & Finalize
Lecture Handout-3
Instructor .Asim Majeed
Java Destructor
While working with classes in Java, constructors are used for initializing the instance
of a class. The memory is allocated for the object using the constructor but after the
object life-cycle is finished and the object is no longer in use, the memory has to be
de-allocated and released. This is where destructor in Java comes into the picture.
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.
The following tasks get executed when a destructor is called.
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.
● 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
Constructors are called when an instance Destructors are called when an object is
of a class is created destroyed or released