How can memory leaks occur in Java even with automatic garbage collection?

Memory leaks in Java occur when objects that are no longer required remain reachable through active references. Since the Garbage Collector can only remove unreachable objects, these unnecessary references prevent memory from being reclaimed, leading to increased memory consumption over time.

Key Points

Example

If objects are continuously added to a static List and never removed, they remain referenced throughout the application's lifetime. As a result, the Garbage Collector cannot reclaim their memory.

import java.util.ArrayList;
import java.util.List;

public class MemoryLeakDemo {

    private static final List<Object> cache = new ArrayList<>();

    public static void main(String[] args) {
        while (true) {
            cache.add(new Object());
        }
    }
}

Interview Tip

A concise interview answer is

Memory leaks can occur in Java when objects are no longer needed but are still referenced by reachable objects. Since the Garbage Collector only removes unreachable objects, these retained references prevent memory reclamation and can lead to excessive memory usage or OutOfMemoryError.

Last updated: 11 November 2026

Previous question: Which algorithms does the JVM use for garbage collection?

Next question: Is Java a 100% object-oriented programming language?

More Core Java - Level II interview questions

All Core Java - Level II questions