Clean • Professional
Finalization was an old Java mechanism that allowed an object to perform cleanup before the Garbage Collector removed it.
It used the finalize() method, which belonged to the Object class.
However, finalize() is deprecated (Java 9+) and removed in modern Java because:
Originally, finalization was meant to:
But later Java introduced try-with-resources and AutoCloseable, which made finalization unnecessary.

When an object becomes unreachable, GC prepares to remove it.
If the class overrides finalize(), JVM may call it.
@Override
protected void finalize() throws Throwable {
System.out.println("Finalize called!");
}
Inside finalize(), you could write code that assigns the object to a static variable — this brings it back to life.
This is one major reason it was deprecated.
If not resurrected, GC deletes the object permanently.
1. Unpredictable Execution : You never know when or if finalize() will run.
2. Causes Delays in Garbage Collection : Objects with finalize() go into a special queue → slower GC.
3. Object Resurrection Issue : An object can become alive again → leads to memory leaks.
4. Performance Overhead : GC becomes slower because it must process finalize-queue objects.
5. Not Reliable for Resource Cleanup : File handles or DB connections may remain open too long.
Java 9 marked it as:
@Deprecated(since="9", forRemoval=true)
Handles automatic closing of resources.
try (FileInputStream fis = new FileInputStream("a.txt")) {
// work
}
class MyResource implements AutoCloseable {
public void close() {
System.out.println("Cleaned!");
}
}
A modern alternative to finalize().
Cleaner cleaner = Cleaner.create();
class Resource implements Runnable {
@Override
public void run() {
System.out.println("Cleaning...");
}
}
This is safe and predictable.