0% found this document useful (0 votes)
39 views3 pages

3 - Destructor & Finalize

The document discusses the concept of destructors in Java, which are special methods for de-allocating memory and releasing resources when an object's life-cycle ends. It explains that Java uses garbage collection to manage memory, automatically deleting unreachable objects, and highlights the differences between constructors and destructors. Additionally, it mentions the finalize() method as an alternative for invoking cleanup actions similar to destructors.

Uploaded by

asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

3 - Destructor & Finalize

The document discusses the concept of destructors in Java, which are special methods for de-allocating memory and releasing resources when an object's life-cycle ends. It explains that Java uses garbage collection to manage memory, automatically deleting unreachable objects, and highlights the differences between constructors and destructors. Additionally, it mentions the finalize() method as an alternative for invoking cleanup actions similar to destructors.

Uploaded by

asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Object Oriented Programming

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.

● 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

Destructors in Java also known as finalizers are non-deterministic. The allocation


and release of memory are implicitly handled by the garbage collector in Java.

Finalizers in Java have to be implicitly invoked since their invocation is not


guaranteed, unlike C# finalizers which get invoked during the .NET run-time.

Let’s take a look at key 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
● Also called when the object instance is no longer eligible for access
● Used for releasing un-managed resources instead of managed resources held by the
object

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

Let’s take a look at a few 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

Let’s try to understand why Destructors are not used in Java.

Constructor vs Destructor: Difference Between A


Constructor and A Destructor
Constructor Destructor

A destructor is used to delete or destroy


A constructor is used to initialize an
the objects when they are no longer in
instance of a class
use

Constructors are called when an instance Destructors are called when an object is
of a class is created destroyed or released

Memory allocation Releases the memory

Overloading is possible Overloading is not allowed

No arguments can be passed in a


They are allowed to have arguments
destructor

Java Finalize() Method


It becomes fairly difficult for any developer to force the execution of a garbage
collector, but there is an alternative to this. We can use the object.finalize() method
which works exactly like a destructor in Java.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy