Quick revision
The short answer
Finalization shows that a lifecycle API tied to garbage collection cannot give timing, ordering or even a guarantee that cleanup happens, and that once code depends on an API, removing it takes decades. So for our own libraries we require explicit lifecycle: resources implement AutoCloseable and are used with try-with-resources, or are managed by a framework's lifecycle callbacks, with Cleaner only as a logged safety net, and every API documents who owns and closes what.
What to remember
In this answer, we saw that finalization failed because it tied resource cleanup to garbage collection, which offers no timing, ordering or completion guarantee, and slows the collector. Because code depended on it, removal took from Java 1.0 to JDK 18 just to reach deprecation for removal. Good lifecycle design is explicit: AutoCloseable with try-with-resources for short-lived resources, framework callbacks for long-lived ones, Cleaner only as a logged safety net, and clear documentation of who owns and closes each resource.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Finalization shows that a lifecycle API tied to garbage collection cannot give timing, ordering or even a guarantee that cleanup happens, and that once code depends on an API, removing it takes decades. So for our own libraries we require explicit lifecycle: resources implement AutoCloseable and are used with try-with-resources, or are managed by a framework's lifecycle callbacks, with Cleaner only as a logged safety net, and every API documents who owns and closes what.
Background to this question
Senior engineers are asked this kind of question because they design the APIs other teams build on for years. The story of finalize() is a cheap way to learn an expensive lesson: it shipped in Java 1.0, was known to be problematic by the early 2000s, was deprecated in Java 9 in 2017, and was only deprecated for removal in JDK 18 in 2022. In this answer, we will look at why the original design failed, why it was so hard to remove, what a sound lifecycle design looks like today, and what rules we would set for the libraries our teams publish.
1What Finalization Promised and Why It Could Not Deliver
The idea in 1995 was attractive. Programmers coming from C++ were used to destructors, methods that run when an object dies. Java had a garbage collector instead of explicit delete, so finalize() was offered as the closest equivalent: the collector would call it before reclaiming the object.
The flaw is that a destructor in C++ runs at a known point, when the object goes out of scope, while the garbage collector runs only when the heap needs space. Those two things have nothing to do with each other. A service can hold thousands of open sockets while using little memory, and the collector has no reason to run. So finalizers gave no timing guarantee, no ordering guarantee between objects, and no guarantee of running before exit at all.
Worse, the mechanism had a cost even for well-behaved programs. Every finalizable object had to be registered at allocation and survive at least one extra collection cycle, and all finalizers ran on one thread. The feature that was supposed to make cleanup safer made the collector slower and made leaks harder to see, because the finalizer usually cleaned up eventually, which masked missing close() calls.
2Why Removing It Took More Than Twenty Years
Once an API is public, every library and application that depends on it becomes a reason not to change it. Removing finalize() would silently turn those libraries' safety nets into leaks, and those leaks would appear in production, not at compile time. So the JDK team had to move carefully: first build replacements, then deprecate, then give everyone tools to measure their dependency, and only then remove.
| Year | Release | Step |
|---|---|---|
| 1996 | Java 1.0 | Object.finalize() ships with the language |
| 1998 | Java 1.2 | Reference types and queues give a better building block |
| 2011 | Java 7 | try-with-resources and AutoCloseable arrive |
| 2017 | Java 9 | finalize() deprecated; java.lang.ref.Cleaner added |
| 2022 | JDK 18 | JEP 421: deprecated for removal, --finalization=disabled |
| future | a later JDK | finalization disabled by default, then removed |
Notice how long each stage waited for the one before it. Deprecation was only possible once Cleaner and try-with-resources existed, and removal is only possible once teams can test without finalization. The lesson for API designers is direct: a lifecycle API is the hardest kind to fix later, because its failures are leaks and resource exhaustion rather than compile errors, so we must get it right the first time.
3What Sound Lifecycle Design Looks Like Today
The modern answer is explicit lifecycle. A class that owns a resource implements AutoCloseable, and the code that creates it decides when it ends, usually with try-with-resources. The resource is released at a known point, on the caller's thread, with exceptions reported rather than swallowed.
- try-with-resourcescaller owns it; closed at the end of the block
- Framework lifecyclecontainer calls
close()or@PreDestroyat shutdown - Explicit close() in finallymanual, only where scoping does not fit
- Cleaner safety netruns after GC; log it, because it means a caller bug
4 layers, from the top down. Step through them one at a time.
For long-lived resources such as connection pools and clients, we hand ownership to a framework. Spring calls close() or a @PreDestroy method when the application context shuts down, and Spring even infers a public close() or shutdown() method on @Bean objects by default. Either way, someone specific is responsible for ending the object's life, and that someone is visible in the code.
Cleaner still has a place, but only as a safety net for callers who forget. When its action runs, it means a caller did not close the resource, which is a bug. So a good library logs a warning when the Cleaner fires, ideally with the stack trace of where the resource was created, captured only in a debug mode because collecting stacks is expensive. Netty's leak detector works on this principle, and it is one of the reasons Netty users find their leaks in testing rather than production.
4The Rules We Would Require of Our Own Libraries
From that history, the rules for our libraries follow. Every type that holds a non-memory resource implements AutoCloseable, and its close() is idempotent, so calling it twice is harmless. No library type overrides finalize(), enforced by a build check. A Cleaner may be registered as a safety net, its action must not reference the owning object, and it must log when it fires so the leak is seen and fixed.
Most important, every API documents ownership. If a method returns a stream, a connection or a client, its documentation says whether the caller must close it, or whether it belongs to the library. If a method accepts a resource, it says whether it takes ownership and will close it, or only borrows it. Many real leaks are not missing close() calls but confusion about who was supposed to make them, such as a wrapper that closes a stream the caller still needed, or a factory whose callers never knew they owned the result.
Finally, we treat lifecycle as part of the public contract and design it before the first release, because changing it later costs every consumer a migration. A library that starts with explicit close() can always add conveniences; a library that starts with implicit cleanup is stuck, as the JDK itself was for twenty years.
5Follow-up questions
Is it ever right to rely on the garbage collector for cleanup?
For memory, yes, that is its job. For anything scarce outside the heap, no. The collector's schedule is driven by heap pressure, so it is unrelated to how many descriptors, sockets or native bytes we hold. Cleaner is acceptable as a fallback, never as the plan.
How would you enforce these rules across many teams?
With tooling rather than review alone: an ArchUnit or Error Prone rule that rejects finalize() overrides, a shared base class or test helper that checks close() is idempotent, a leak-detecting mode in the library that logs Cleaner activity during tests, and a documentation template with an ownership line for every public method that returns or accepts a resource.
What does idempotent close() mean, and why require it?
It means calling close() a second time does nothing and does not throw. Resources get closed from several places, such as a try-with-resources block and a wrapper's close(), and an exception from the second call would hide the real error or break shutdown.
6Conclusion
In this answer, we saw that finalization failed because it tied resource cleanup to garbage collection, which offers no timing, ordering or completion guarantee, and slows the collector. Because code depended on it, removal took from Java 1.0 to JDK 18 just to reach deprecation for removal. Good lifecycle design is explicit: AutoCloseable with try-with-resources for short-lived resources, framework callbacks for long-lived ones, Cleaner only as a logged safety net, and clear documentation of who owns and closes each resource.
Open with
The finalize story taught me that a lifecycle API tied to garbage collection cannot work, because the collector runs on heap pressure, not on how many files or sockets we hold.
Then, if they want more
Finalizers gave no timing, no ordering and no guarantee they ran at all, they slowed allocation and collection, and they hid missing close calls because cleanup usually happened eventually.
It also taught me how expensive a bad lifecycle API is: it shipped in 1.0, was deprecated in Java 9, and only deprecated for removal in JDK 18, because removing it turns every dependent library's safety net into a production leak. So for my own libraries I require explicit lifecycle.
Any type that holds a non-memory resource implements AutoCloseable with an idempotent close, short-lived uses go in try-with-resources, and long-lived ones like clients and pools are owned by the framework, for example as Spring beans closed at shutdown. No finalize overrides, enforced in the build.
A Cleaner is allowed only as a safety net, its action must not reference the owner, and it logs when it fires, since that means a caller forgot to close. And every API documents ownership: whether the caller must close what it gets back, and whether a method takes ownership of what it is given.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 18, Java 21, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Finalization was part of Java from 1.0 and took over twenty years to be deprecated for...": https://scrivio-interview-library.netlify.app/java/object-contracts/4c44f1030c/