Time to say goodbye to the finalize method in Java

Anish Antony
Javarevisited
Published in
5 min readMar 30, 2021

--

Now the finalize method is deprecated from Java. Over this article let's explore why java deprecated the finalize method, what are the alternative solution they introduced instead of finalizing method, What are its advantages …etc

Photo by Jasmin Sessler on Unsplash

The Object class has a finalize() method, which is automatically called by GC before it attempts to remove the object from the heap. In Java 9, the finalize() method has been deprecated. The finalization mechanism is inherently problematic and can lead to performance issues, deadlocks, and hangs. The java.lang.ref.Cleaner and java.lang.ref.PhantomReference provide more flexible and efficient ways to release resources when an object becomes unreachable.

What is the use of finalize() method?

The finalize() method of Object class is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection, so as to perform clean-up activity.

Finalizers get invoked when JVM figures out that this particular instance should be garbage collected. Such a finalizer may perform any operations, including bringing the object back to life.

--

--

Anish Antony
Javarevisited

Fullstack Developer | Blogger | Experience on Java, Python, React, Angular, Golang | http://www.behindjava.com