Fundamentals 9 min read

Understanding Java Garbage Collection: Goals, Algorithms, and Timing

This article explains Java garbage collection by outlining its purpose, the criteria for reclaimable objects through reference counting and reachability analysis, when collection occurs, and the main GC algorithms—including mark‑sweep, copying, mark‑compact, and generational collection—along with common JVM collectors.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java Garbage Collection: Goals, Algorithms, and Timing

Java GC

Goal

When encountering a problem or knowledge point, we first clarify what issue it solves. For Java GC, the purpose is to reclaim memory because memory is limited; as objects are created, the heap grows, potentially causing out‑of‑memory errors. Therefore GC must identify which objects are reclaimable and when to run.

Which objects can be reclaimed

In short, objects that are no longer useful can be reclaimed. Two main techniques determine uselessness: reference counting and reachability analysis.

Reference Counting

Reference counting increments a counter each time an object is referenced and decrements when a reference is removed. When the count reaches zero, the object is considered dead and can be reclaimed. However, cyclic references keep counts above zero, causing memory leaks.

Reachability Analysis

Reachability analysis starts from GC roots (objects on the Java stack, static fields in the method area, and native method stack objects) and traverses references. Objects not reachable from any root are considered garbage and eligible for collection.

In the diagram, objects A, B, C are reachable, while D, E, F are not and thus are garbage.

When to collect

GC is typically triggered when the JVM needs to allocate a new object and there is insufficient free space in the heap.

How to collect

Various algorithms implement GC, including mark‑sweep, copying, mark‑compact, and generational collection.

Mark‑Sweep

Mark‑sweep first marks live objects, then sweeps away unmarked memory, which can lead to fragmentation.

Copying

The copying algorithm divides the heap into two halves; live objects are copied to the other half during GC, and the original half is cleared, eliminating fragmentation but halving usable memory.

Mark‑Compact

Mark‑compact combines marking and then compacts live objects toward one end of the heap, reducing fragmentation while still requiring a move phase.

Generational Collection

The most common approach partitions the heap into young and old generations. The young generation uses the copying algorithm, while the old generation uses mark‑compact, based on object age.

Garbage Collectors

Typical JVM collectors include Serial, ParNew, Parallel Scavenge, SerialOld, ParallelOld, CMS, and G1.

Serial Collector (single‑threaded, copying)

ParNew (Serial + multithreaded)

Parallel Scavenge (multithreaded copying, high throughput)

SerialOld (single‑threaded mark‑compact)

ParallelOld (multithreaded mark‑compact)

CMS (multithreaded mark‑sweep)

G1 Collector

For most developers, focusing on CMS and G1 provides sufficient coverage.

JavaJVMMemory ManagementGarbage CollectionGC AlgorithmsReference CountingReachability Analysis
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.