2. CONTENT
• DESTRUCTOR
• WORKING OF DESTRUCTORS
• NEED FOR DESTRUCTORS
• OBJECT DESTRUCTION IN JAVA
• WAYS TO HANDLE CLEANUP IN JAVA
• GARBAGE COLLECTION IN JAVA
• ADVANTAGES OF DESTRUCTORS
• HOW DESTRUCTOR WORKS
• FINALIZE() METHOD (DEPRECATED)
• BEST PRACTICES FOR RESOURCE CLEANUP
• CONCLUSION
3. DESTRUCTOR
• A destructor is a special method that is automatically
invoked when an object is no longer needed, mainly used
to free resources like memory, database connections, or
file handlers.
• Java does not have explicit destructors because
Java automatically manages memory using Garbage
Collection (GC).
4. How does destructor work?
• When the object is created it occupies the space in the heap.
These objects are used by the threads. If the objects are no
longer is used by the thread it becomes eligible for the
garbage collection. The memory occupied by that object is
now available for new objects that are being created. It is
noted that when the garbage collector destroys the object,
the JRE calls the finalize() method to close the connections
such as database and network connection.
5. Why is a Destructor Needed?
• A destructor is typically needed to:
1.Release memory occupied by unused objects.
2.Close files, database connections, or network sockets to
prevent resource leaks.
3.Free system resources, such as locks or UI components.
6. How Does Java Handle Object Destruction?
• Java does not have explicit destructors because it has Garbage
Collection (GC), which automatically deallocates unused objects.
• Garbage Collection is not predictable. The JVM decides when to run
the Garbage Collector, meaning objects are not destroyed immediately
after they become unreachable.
7. • Java’s garbage collector automatically manages the
lifecycle of objects.
• When no references point to an object, it becomes eligible
for garbage collection.
• The garbage collector reclaims memory at its discretion,
making memory management automated and simplifying
development.
8. How to Handle Cleanup in Java?
• Since Java does not have destructors, it provides alternative mechanisms for
cleanup:
1.Garbage Collection (Automatic)
2.finalize() method (Deprecated after Java 9) The Java compiler shows warnings if
you use it.
3.try-with-resources (Best Practice)
4.Explicit close() methods (Manual Cleanup)
9. Garbage Collection in Java
• What is Garbage Collection?
• Garbage Collection is an automatic memory management feature
in Java.
• It reclaims memory by destroying objects that are no longer in use.
• The JVM decides when to run the garbage collector, making it non-
deterministic.
• Advantages:
• Reduces the risk of memory leaks.
• Simplifies memory management for developers.
10. Advantages of Destructors
• Automatic Resource Management: By automating the process of freeing
up memory and other resources, Java's garbage collector lowers the
possibility of memory leaks and improper resource management.
• Simplifies Programming: By eliminating the need for developers to
manually monitor and release memory, programming becomes simpler
and less prone to error.
• Enhances Performance: By effectively handling memory allocation and
deallocation, automated memory management can aid in performance
optimization.
11. Java finalize() Method
• Java provides an alternative way to do the same.
• The Java Object class provides the finalize() method that
works the same as the destructor. The syntax of the finalize()
method is as follows:
• Before Java 9, Java allowed the use of the finalize() method
to define cleanup tasks before the object is garbage
collected.
12. Finalize() Method (Deprecated)
• Java finalize() Method
• The finalize() method was used to perform cleanup tasks before an object was garbage collected.
• Syntax:
• java
• Copy
• protected void finalize() throws Throwable
• {
• // Cleanup code
• }
• Deprecated after Java 9: Due to its unreliable execution and performance overhead.
• Alternative: Use try-with-resources or explicit close() methods for resource cleanup.
13. Best Practices for Resource Cleanup
• Best Practices for Handling Cleanup in Java:
1.Use try-with-resources: Automatically closes resources like files, sockets, and database
connections.
2.java
try (FileInputStream fis = new FileInputStream("file.txt"))
{
// Use the resource
}
catch (IOException e)
{ e.printStackTrace();
}
2. Implement AutoCloseable: Create custom resources that can be used with try-with-
resources.
3. Avoid finalize(): Use explicit cleanup methods instead.
14. Conclusion
• Key Takeaways:
• Java does not have explicit destructors but uses Garbage
Collection for automatic memory management.
• The finalize() method is deprecated; use try-with-
resources or close() methods for cleanup.
• Automatic memory management simplifies programming
and reduces the risk of memory leaks.
• Always follow best practices for resource cleanup to ensure
efficient and reliable code.