Fundamentals 15 min read

Why Java’s Garbage Collection Matters: Algorithms, Roots, and Memory Management

Java’s garbage collection, a critical yet often overlooked feature, encompasses various algorithms such as reference counting, reachability analysis, mark‑sweep, copying, and generational collection, each with distinct mechanisms, trade‑offs, and impacts on memory regions like Eden, Survivor, and Old spaces.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Why Java’s Garbage Collection Matters: Algorithms, Roots, and Memory Management

What is Garbage Collection?

Garbage Collection (GC) is the process of reclaiming memory occupied by objects that are no longer reachable, preventing memory leaks and making reclaimed space available for new allocations.

History

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

Reference Counting Algorithm

The reference counting algorithm stores a counter in each object header that tracks how many references point to the object. When the count drops to zero, the object becomes eligible for reclamation. String m = new String("jack"); Here, the string "jack" has one reference (m). Setting m = null; reduces the reference count to zero, making the object collectible.

m = null;
Reference Counting
Reference Counting

Reachability Analysis (GC Roots)

Reachability analysis determines object liveness by tracing from a set of root objects (GC Roots). An object is considered live if it is reachable from any root; otherwise, it is reclaimed.

Objects referenced from the JVM stack (local variables)

Static fields in the method area

Constants in the method area

Objects referenced from native methods (JNI)

GC Roots
GC Roots

Mark‑Sweep Algorithm

Mark‑Sweep is the most basic GC algorithm. It first marks all reachable objects, then sweeps away the unmarked (dead) objects, freeing their memory.

Mark Sweep
Mark Sweep

Copying Algorithm

The copying algorithm divides the heap into two equal spaces. Live objects are copied from the active space to the other space, after which the entire active space is cleared, eliminating fragmentation.

Copying Algorithm
Copying Algorithm

Mark‑Compact Algorithm

Mark‑Compact combines marking with a compaction phase: after marking live objects, it slides them toward one end of the heap, then clears the remaining space, thus avoiding fragmentation while preserving object order.

Mark Compact
Mark Compact

Generational Collection

Generational collection splits the heap into young and old generations. New objects are allocated in the Eden space; most die quickly and are reclaimed by minor GCs using the copying algorithm. Objects that survive many minor GCs are promoted to the old generation, where mark‑sweep or mark‑compact is used.

Generational Collection
Generational Collection

Java Heap and Memory Spaces

The Java heap is divided into the young generation (Eden + two Survivor spaces) and the old generation. Eden holds newly created objects; when it fills, a Minor GC moves surviving objects to a Survivor space. Two Survivor spaces alternate to avoid fragmentation. Objects that survive a configurable number of Minor GCs (default 15) are promoted to the old generation, which is collected by Major GCs (stop‑the‑world). Large objects and long‑lived objects are allocated directly in the old generation to reduce copying overhead.

Java Heap
Java Heap

Understanding these GC algorithms and memory regions helps developers tune JVM performance, avoid memory leaks, and choose appropriate GC settings for their applications.

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
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

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.