Why is finalize() discouraged, and what replaces it for resource cleanup?
Reviewed by Gurusankar M.
Short Answer
finalize() runs on an unpredictable schedule (or never), can resurrect objects, slows GC, and may not run at all — so it can't be trusted to release resources. It's deprecated; use try-with-resources/AutoCloseable, or Cleaner/PhantomReference as a safety net.
On this page
Concept Made Simple
Coffee Chat Question
“Why is finalize() discouraged, and what replaces it for resource cleanup?”
Remember It Faster
Mind Map Answer
- Unpredictable
- no guarantee it ever runs
- Slow
- extra GC cycle to finalize
- Risky
- object resurrection, exceptions ignored
- Use instead
- AutoCloseable + try-with-resources
Think Beyond the Expected
What If?
If close() is deterministic, why add a Cleaner at all?
As a last-resort safety net: if a caller forgets close(), the Cleaner releases the native resource when the object becomes unreachable — preventing a permanent leak. The primary path is still explicit close().
Real World
Classes wrapping native/OS resources (files, sockets, off-heap buffers) implement AutoCloseable for deterministic release and register a Cleaner purely as a backstop — never relying on finalize().
Interviewer's Expectation
Keywords they're listening for:
- finalize deprecated/unreliable
- try-with-resources primary
- Cleaner/PhantomReference backstop
- no resurrection
Common Mistakes
- Releasing resources in finalize()
- Relying on GC timing for cleanup
- Capturing the host object inside the Cleaner action (prevents collection)
Best Practices
- Implement AutoCloseable; close deterministically
- Use Cleaner only as a safety net
- Never depend on finalize() — it's removed in newer JDKs
Follow-up Questions
- How does java.lang.ref.Cleaner work?
- Why must the Cleaner action not reference the object it cleans?
- What's the difference between weak/soft/phantom references?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Memory/GC (Core Java) Interview question: "Why is finalize() discouraged, and what replaces it for resource cleanup?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair