Fundamentals 15 min read

Why Java’s Garbage Collection Matters: From Roots to Generational Algorithms

This article explains Java garbage collection in depth, covering its definition, historical origins, reference‑counting and reachability algorithms, GC roots, memory regions such as Eden, Survivor and Old, and the main collection strategies like Mark‑Sweep, Copying, Mark‑Compact and Generational collection.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Java’s Garbage Collection Matters: From Roots to Generational Algorithms

What Is Garbage Collection?

Garbage Collection (GC) automatically frees memory occupied by objects that are no longer reachable, preventing memory leaks and allowing the JVM to reuse heap space.

Historical Background

The concept of GC was first introduced in 1960 by the MIT Lisp project, long before Java existed, showing that GC is not a Java‑specific invention.

Reference Counting Algorithm

Reference counting stores a counter in each object header indicating how many references point to it. When the count drops to zero, the object can be reclaimed.

String m = new String("jack");
// m holds a reference to "jack"
m = null; // reference count becomes 0, eligible for GC

Example demonstrating a circular reference that prevents collection:

public class ReferenceCountingGC {
    public Object instance;
    public ReferenceCountingGC(String name) {}
}
public static void testGC() {
    ReferenceCountingGC a = new ReferenceCountingGC("objA");
    ReferenceCountingGC b = new ReferenceCountingGC("objB");
    a.instance = b;
    b.instance = a;
    a = null;
    b = null;
}

Because a and b reference each other, their counters never reach zero, so they cannot be reclaimed.

Reachability Analysis Algorithm

Reachability analysis starts from a set of GC Roots (e.g., stack variables, static fields, JNI references) and marks all objects reachable via reference chains. Objects not reachable from any root are considered garbage.

GC Roots in Java

Objects referenced from the JVM stack (local variables).

Objects referenced by static fields in the method area.

Objects referenced by constants in the method area.

Objects referenced by native (JNI) code.

Java Memory Areas

The Java heap is divided into the Young Generation and the Old Generation. The Young Generation consists of the Eden space and two Survivor spaces (From and To).

Eden Space

Most short‑lived objects are allocated in Eden. When Eden fills up, a Minor GC occurs, reclaiming unreachable objects and moving survivors to a Survivor space.

Survivor Spaces

Survivor spaces act as a buffer between Eden and the Old Generation. Objects that survive several Minor GCs are promoted to the Old Generation, reducing the frequency of Major GCs.

Old Generation

The Old Generation holds long‑lived objects and is collected by Major GC, which typically uses a Stop‑The‑World pause.

Garbage‑Collection Algorithms

Mark‑Sweep

Marks all reachable objects, then sweeps away unmarked ones. Simple but can cause memory fragmentation.

Copying (Copy‑On‑Write)

Divides the heap into two equal halves; live objects are copied to the active half, and the other half is cleared, eliminating fragmentation.

Mark‑Compact (Mark‑Sweep‑Compact)

After marking, all live objects are moved (compacted) to one end of the heap, and the remaining space is reclaimed, solving fragmentation at the cost of extra copying.

Generational Collection

Combines the above algorithms: the Young Generation uses Copying for its high turnover, while the Old Generation uses Mark‑Sweep or Mark‑Compact because objects there have higher survival rates.

Mark‑Sweep diagram
Mark‑Sweep diagram
Copying algorithm diagram
Copying algorithm diagram
Mark‑Compact diagram
Mark‑Compact diagram
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 Collectionreference countingGenerational GC
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.