Last Updated: June 6, 2026
A memory leak in Java has a specific meaning. Java has a garbage collector. It does not forget to free things. What it does is free objects that are no longer reachable from any GC root, and the leak shows up when an object stays reachable even though the program is logically done with it. The GC sees a chain of references that still leads back to a thread, a static field, or a JNI handle, so it leaves the object alive. Multiply that mistake across a long-running server and the heap grows until the JVM throws OutOfMemoryError.
This lesson walks through the canonical leak patterns in Java code (each shown with broken and fixed e-commerce examples), the tools you use to spot a leak in a running JVM, and the prevention habits that stop most leaks before they ever ship.