Fundamentals 9 min read

Overview of Garbage Collection Algorithms and JVM Garbage Collectors

This article provides a comprehensive overview of garbage collection techniques, covering reference counting, reachability analysis, generational management, copy, mark‑sweep, and mark‑compact algorithms, and explains the JVM's serial, parallel, CMS, and G1 collectors along with their operational characteristics and trade‑offs.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Overview of Garbage Collection Algorithms and JVM Garbage Collectors

Garbage collection (GC) is a core responsibility of heap memory management, encompassing both memory allocation and reclamation, and is typically implemented using two main families of algorithms: reference counting and reachability (root set) analysis.

Reference counting adds a counter to each object, incrementing on new references and decrementing when references are lost; objects with a zero count are reclaimed, but this approach must handle cyclic dependencies, which languages like Python resolve using a mark‑and‑sweep fallback.

Reachability analysis starts from a set of root references and traverses the object graph; objects not reachable from any root are considered dead and can be collected. The JVM adopts this method for its own GC.

GC algorithms have evolved into several classifications: copy, mark‑sweep, and mark‑compact implementations; collection strategies such as serial, parallel, and concurrent; and memory management schemes like generational versus non‑generational.

Generational management divides the heap into young and old generations, exploiting the observation that most objects die young. Young‑generation objects are typically reclaimed using a copy algorithm, while old‑generation objects often use mark‑compact techniques.

The copy algorithm can be implemented with two or more regions (e.g., Eden, Survivor0, Survivor1). Objects are initially allocated in Eden; after a collection, live objects are moved to a survivor space, and the process repeats, as illustrated in the accompanying figures.

Copy algorithm first collection
Copy algorithm first collection

Figure 1‑1: First copy‑algorithm collection.

Copy algorithm second collection
Copy algorithm second collection

Figure 1‑2: Second copy‑algorithm collection.

Mark‑sweep traverses reachable objects from the roots, marks them, and then sweeps away unmarked objects, but it can cause memory fragmentation.

Mark‑sweep algorithm
Mark‑sweep algorithm

Figure 1‑3: Mark‑sweep algorithm.

Mark‑compact adds a compaction phase after marking, relocating live objects to eliminate fragmentation.

JVM garbage collectors built on these principles include:

Serial collector – single‑threaded, stop‑the‑world (STW) pauses; young generation uses copy, old generation uses mark‑compact.

Parallel collector – multi‑threaded, STW pauses; same generational algorithms as serial.

Concurrent Mark‑Sweep (CMS) – combines concurrent marking and sweeping with brief STW phases for initial and final marks; suited for old generation, while young generation may use parallel collection.

Garbage‑First (G1) – partitions the heap into many regions, performs parallel copy collection for young regions, and incremental mixed collections for selected old regions, aiming for predictable pause times and high throughput on multi‑CPU, large‑memory servers.

Serial collector diagram
Serial collector diagram

Figure: Serial collector.

Parallel collector diagram
Parallel collector diagram

Figure: Parallel collector.

CMS collector diagram
CMS collector diagram

Figure: Concurrent Mark‑Sweep (CMS) collector.

G1 collector diagram
G1 collector diagram

Figure: Garbage‑First (G1) collector.

G1 also treats large objects specially by allocating them directly into old‑generation regions, and it integrates concepts from train algorithms, CMS, and oldest‑first policies to optimize region selection.

GC algorithm advantages and disadvantages
GC algorithm advantages and disadvantages

Table: Advantages and disadvantages of basic GC algorithms.

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.

JVMMemory ManagementGarbage CollectionGenerational GCMark‑SweepG1Copy Algorithm
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

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.