What is the finalize() method in Java?
`finalize()` is a method defined on `java.lang.Object` that the garbage collector is permitted to call on an object before reclaiming its memory, giving the object a chance to release native resources or perform cleanup. It was part of Java from version 1.0.
The mechanism works as follows: when the GC determines an object is unreachable, it places the object on a finalization queue. A dedicated finalizer thread dequeues and calls `finalize()` on each object. After finalization, the GC can reclaim the object's memory on the next cycle.
In practice, `finalize()` has severe problems that led to its deprecation in Java 9 and its removal/forRemoval marking in Java 18:
1. Unpredictability. There is no guarantee about when — or even whether — `finalize()` will be called. The JVM specification allows it to never run, including on normal JVM exit. 2. Performance impact. Objects with non-trivial finalizers require at minimum two GC cycles to collect, prolonging their lifetime and increasing memory pressure. 3. Resurrection risk. A finalizer can store `this` in a static field, resurrecting the object. This complicates GC logic significantly. 4. Exception swallowing. Any exception thrown from `finalize()` is silently ignored, making debugging difficult. 5. Security. Malicious finalizers in subclasses can exploit the finalization delay to access resources that should have been closed.
The correct alternatives are: - `try-with-resources` with `AutoCloseable` — deterministic, exception-safe cleanup via `close()`. - `java.lang.ref.Cleaner` (Java 9+) — a phased, safer alternative to finalization for native resource cleanup when `AutoCloseable` is impractical. - Explicit `close()` or `dispose()` patterns for long-lived resources.
For interviews: know what finalize does, why it is problematic, and what replaced it.
Explains that finalize() is called before GC cleanup and lists at least two of its reliability problems.
Also covers the two-GC-cycle collection cost, the Java 9 deprecation, and the correct modern alternatives (try-with-resources, Cleaner).
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment