intermediateJVM Internals & Memory

Why is Object.finalize() dangerous, and what should you use instead?

finalize() runs only when the GC decides to, with no guaranteed timing — a hung finalizer on one object can stall the GC for the whole JVM. It's deprecated since Java 9; use try-with-resources (AutoCloseable) for guaranteed, deterministic cleanup instead.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to What is Java Flight Recorder and how is it different from a heap dump?← Back to all Core Java questions