Course contentsShow
← Java
Lesson 577 of 2,87013. The Object Class and Common MethodsFree lesson

Deprecation of finalize in Java 9+

Understand why finalize was deprecated and how to migrate legacy code that depends on it.

Deprecation of finalize in Java 9+

You'll learn why Java deprecated finalize() and how to migrate legacy code to modern resource management techniques.

For years, Java developers relied on Object.finalize() as a safety net to release non-memory resources (like file handles or native memory) before garbage collection. However, this mechanism proved fundamentally flawed and is being actively phased out of the Java language.

Java 9 Deprecation vs. JDK 18 Deprecation for Removal

The phase-out of finalization is happening in major steps:

  1. Java 9: The finalize() method was marked as @Deprecated. This served as a strong warning to stop writing new finalizers, but the feature was not yet slated for destruction.
  2. JDK 18 (JEP 421): Finalization was officially upgraded to deprecated for removal. This means finalization is planned to be removed in a future Java release.

Currently, finalization remains enabled by default in modern JDKs. However, JEP 421 introduced the ability to disable it to test your application's readiness. You can run your application with the command-line flag --finalization=disabled to see how it behaves once finalization is permanently removed.

The Flaws of Finalization

JEP 421 outlines several critical flaws that led to this decision:

  • Unpredictable latency: There is no guarantee when, or even if, a finalizer will run. This arbitrary delay causes resource leaks because the underlying operating system resources remain open much longer than necessary.
  • Garbage Collection impact: Finalizers add unpredictable latency and unnecessary work to the Garbage Collector.
  • Security risks: Finalizers can be exploited in "finalizer attacks" to resurrect objects whose constructors threw exceptions.
  • Unconstrained behavior: Finalizers run on unspecified threads in an arbitrary order, leading to complex threading bugs and deadlocks.

Modern Alternatives

If you are migrating legacy code that uses finalize(), you should replace it with these modern techniques:

  1. try-with-resources and AutoCloseable: This is the primary and most reliable way to manage resources. It guarantees deterministic, immediate cleanup the moment the block is exited.
  2. java.lang.ref.Cleaner: If you strictly need a background "safety net" in case a developer forgets to call close(), the Cleaner API (introduced in Java 9) handles object cleanup without the security and performance flaws of finalization.

Modern Approach Example:

public class ModernResource implements AutoCloseable {
    
    public void doWork() {
        System.out.println("Working...");
    }

    // Modern approach: Implement AutoCloseable instead of finalize()
    @Override
    public void close() {
        System.out.println("Resource explicitly and predictably closed.");
    }

    public static void main(String[] args) {
        // Deterministic cleanup using try-with-resources
        try (ModernResource resource = new ModernResource()) {
            resource.doWork();
        } // close() is automatically invoked right here
    }
}

Key takeaway: The finalize() method was deprecated in Java 9 and deprecated for removal in JDK 18 (JEP 421) due to unpredictable latency, resource leaks, and security risks; replace it with try-with-resources and java.lang.ref.Cleaner.