Fundamentals 7 min read

Four JVM Garbage Collection Algorithms Explained

This article introduces the four main JVM garbage collection algorithms—Mark‑Sweep, Copying, Mark‑Compact, and Generational collection—explaining their mechanisms, suitable scenarios, advantages, and drawbacks, and summarizing how they are applied to young and old generations in modern Java virtual machines.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Four JVM Garbage Collection Algorithms Explained

Previously I discussed the JVM memory model; now I continue with an overview of the four JVM garbage collection algorithms.

Garbage Collection Algorithms

1. Mark‑Sweep

The Mark‑Sweep algorithm consists of two phases: the mark phase , where GC roots are traversed to mark all reachable objects, and the clear phase , where all unmarked objects are reclaimed.

Suitable scenarios:

Efficient when many objects are short‑lived.

Often used for the old generation (tenured space).

Drawbacks:

Can cause memory fragmentation, leading to premature GC for large objects.

Requires scanning the entire heap twice (once for marking, once for clearing).

2. Copying Algorithm

During a collection, live objects are copied from the current region to a new region; the original region is then reclaimed in its entirety.

Current commercial JVMs use this algorithm for the young generation.

Suitable scenarios:

When the number of live objects is relatively small.

Only one full heap scan is needed (mark and copy).

Ideal for the young generation, where most objects die quickly.

Drawbacks:

Requires an additional empty memory region.

Objects must be copied, adding overhead.

3. Mark‑Compact

Similar to Mark‑Sweep, but after marking reachable objects, it compacts them toward one end of the heap, eliminating fragmentation without needing a second memory region.

It is typically used for the old generation where many objects survive multiple collections.

4. Generational Collection

Modern JVMs divide the heap into generations—young (Eden + Survivor spaces) and old (tenured). Each generation uses the algorithm best suited to its object lifespan.

Young generation objects are allocated in Eden; when Eden fills, live objects are copied to Survivor0, then to Survivor1, and eventually promoted to the old generation. Minor GCs (young‑gen collections) happen frequently, while Major GCs (full collections) occur when the old generation fills.

Garbage Collection Summary

Young generation: Uses the Copying algorithm to quickly reclaim short‑lived objects.

Old generation: Uses Mark‑Sweep or Mark‑Compact, depending on fragmentation and live‑object density.

The generational approach combines these algorithms to achieve efficient memory management for typical Java workloads.

JVMMemory ManagementGarbage CollectionGenerational GCmark-sweepMark-CompactCopying
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

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.