0Pricing
Java Academy · Lesson

Detecting and Fixing Memory Leaks

Identify common memory leak patterns (static collections, listeners, caches) and fix them.

What Is a Memory Leak in Java?

A Java memory leak occurs when objects are no longer needed but remain strongly reachable, preventing GC from reclaiming them. The heap grows until an OutOfMemoryError is thrown.

Static Collection Accumulation

A static field holding a growing collection is a classic leak. Objects added but never removed stay alive for the lifetime of the application.

public class Cache {
    // LEAK: static list grows forever if items are never removed
    private static final List<Object> items = new ArrayList<>();
    public static void add(Object o) { items.add(o); }
    // Fix: add a remove() method or use a bounded cache
}

All lessons in this course

  1. JVM Heap Regions and Object Lifecycle
  2. GC Algorithms: Serial, G1, ZGC, Shenandoah
  3. Detecting and Fixing Memory Leaks
  4. GC Tuning Flags and JVisualVM Profiling
← Back to Java Academy