Software Engineering Glossary

Garbage Collection

Also known as: GC Automatic Memory Management Java Garbage Collection

Garbage collection (GC) is the JVM’s automatic memory management. Instead of freeing memory by hand, you let objects go out of scope and the collector reclaims the heap memory used by objects that are no longer reachable. Most JVM collectors are generational: new objects live in the young generation and are cleared by cheap minor collections, while long-lived objects are promoted to the old generation and collected less often. Modern Java defaults to the G1 collector, with ZGC and Shenandoah available for very low pause times.

Key Takeaways

  • GC reclaims only unreachable objects. Objects still referenced, for example held in a static map, are never collected.
  • Generational collectors focus effort on the young generation, where most objects die quickly.
  • GC frees you from manual memory management but does not make memory leaks impossible.
  • Collector choice and heap sizing are the core of Java performance tuning, and always trade off throughput, latency, and memory.

How It Works

  1. New objects are allocated in the young generation (Eden).
  2. A minor GC clears dead young objects and moves survivors between survivor spaces.
  3. Objects that survive enough collections are promoted to the old generation.
  4. A major or full GC collects the old generation, which is more expensive and can pause application threads.

Where It Is Used

  • G1 (Garbage-First) is the default collector in modern OpenJDK and Oracle JDK.
  • ZGC and Shenandoah keep pauses to a few milliseconds even on very large heaps.
  • An OutOfMemoryError: Java heap space means the GC could not free enough room on the heap.

Related glossary terms