Fundamentals 12 min read

Master Java Garbage Collection: Visual Guide to Algorithms and Roots

This article explains how the JVM determines object liveness, describes reference counting and reachability analysis, lists GC Roots, and provides clear visual and code examples of the main garbage‑collection algorithms—mark‑sweep, copy, mark‑compact, and generational collection—highlighting their advantages, drawbacks, and typical use cases.

JavaEdge
JavaEdge
JavaEdge
Master Java Garbage Collection: Visual Guide to Algorithms and Roots

Object Liveness Determination

Before reclaiming memory the JVM must decide whether an object is still alive. Two classic approaches are used.

Reference Counting

Each object stores a counter that is incremented when a new reference is created and decremented when a reference is cleared. When the counter reaches zero the object can be reclaimed.

Problems:

Every reference update incurs a performance cost.

Cyclic references never reach zero, causing memory leaks. Example:

public void reference() {
  A a = new A();
  B b = new B();
  a.instance = b;
  b.instance = a;
}

After the method returns the stack references disappear, but the two heap objects still reference each other, so their counters remain non‑zero and they cannot be collected.

Reference counting example
Reference counting example

Reachability Analysis (Tracing)

The JVM’s default algorithm starts from a set of GC Roots . Any object reachable via a chain of references from these roots is considered alive; objects without such a path are eligible for collection.

Typical GC Roots in Java include:

Objects referenced from the JVM stack (local variables).

Static fields in the method area.

Constants in the method area.

Objects referenced by native (JNI) methods.

Internal JVM references such as Class objects for primitive types, permanent exception objects, and the system class loader.

Objects held by synchronized locks.

JMXBean and JVMTI callbacks registered inside the JVM.

Temporary roots needed for generational and partial collections.

Reachability analysis
Reachability analysis

Garbage‑Collection Algorithms

Mark‑Sweep

When the heap’s free space is exhausted a stop‑the‑world (STW) pause occurs. The algorithm then performs two phases:

Mark : Starting from GC Roots, traverse the object graph and mark all reachable objects.

Sweep : Scan the entire heap and reclaim any unmarked objects.

Drawbacks: STW pauses, low throughput in both phases, and memory fragmentation after sweeping.

Mark‑Sweep process
Mark‑Sweep process

Copying (Semispace) Algorithm

Used mainly in the young generation, the heap is split into two equal spaces. Live objects are copied from the active space to the free space during a GC pause, after which the former space is cleared.

Advantages: eliminates fragmentation. Drawbacks: only half of the memory is usable at any time, and copying many live objects can be costly when survival rates are high.

Copying algorithm
Copying algorithm

Mark‑Compact (Mark‑Sweep‑Compact) Algorithm

Similar to mark‑sweep but adds a compaction step. After marking live objects, the algorithm moves them toward one end of the heap, updates all references, and then frees the remaining space.

This reduces fragmentation and improves memory locality, but incurs extra overhead for moving objects and updating references.

Mark‑Compact process
Mark‑Compact process

Generational Collection

Modern JVMs combine the above algorithms based on object age. The heap is divided into a young generation and an old generation.

Young generation: predominantly uses the copying algorithm because most objects die quickly.

Old generation: uses either mark‑sweep or mark‑compact, as objects tend to survive longer and copying would be inefficient.

All generational collectors still rely on a stop‑the‑world pause to start the GC thread.

Generational collection layout
Generational collection layout

Comparison of Algorithms

Execution Efficiency : Copying is fastest, followed by mark‑sweep, then mark‑compact.

Memory Utilization : Mark‑compact and mark‑sweep achieve higher utilization; copying uses only half of the allocated space.

Memory Compaction : Copying and mark‑compact produce compact memory layouts; mark‑sweep leaves fragmentation.

Choosing the appropriate algorithm depends on object survival rates, pause‑time requirements, and overall memory usage patterns.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMMemory ManagementGarbage CollectionGC Algorithms
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

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.