Object Lifecycle and Reachability
Java automatically manages object memory. An object becomes eligible for garbage collection when it is no longer reachable from live roots.
Object object = new Object();
object = null;
Setting a variable to null does not force collection. Static fields, thread locals, caches, listeners, and long-lived collections can accidentally retain objects.
A memory leak in a garbage-collected language is usually a logical retention problem rather than failure to manually free memory.
Key Takeaways
- GC eligibility is based on reachability.
- A garbage-collected application can still leak memory through unintended retention.
- Calling GC explicitly is rarely an appropriate leak fix.