Fundamentals 7 min read

Understanding Java Garbage Collection: Reference Counting, Reachability Analysis, and Major GC Algorithms

This article explains how Java determines which objects are eligible for garbage collection using reference counting and reachability analysis, describes the main garbage‑collection algorithms such as mark‑sweep, mark‑compact, and mark‑copy, and outlines the generational GC process including MinorGC and survivor spaces.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java Garbage Collection: Reference Counting, Reachability Analysis, and Major GC Algorithms

What objects need to be reclaimed?

Java uses two basic techniques to decide if an object should be collected: the reference counting algorithm , which tracks how many references point to an object, and the reachability analysis algorithm , which starts from a set of GC Roots and follows reference chains to determine live objects.

Important garbage‑collection algorithms

Mark‑Sweep : first marks all reachable objects, then sweeps (clears) the unmarked ones, leaving fragmented free space.

Mark‑Compact (Mark‑整理) : after marking, moves all live objects to one end of the heap and compacts free space, eliminating fragmentation but requiring reference updates.

Mark‑Copy : divides the heap into two halves; live objects are copied from the active half to the other, then the original half is cleared, avoiding fragmentation at the cost of needing double memory.

Garbage‑collection workflow in the JVM

The JVM employs a generational GC strategy: the young generation (Eden + Survivor spaces) is collected frequently (MinorGC), while the old generation is collected less often.

During a MinorGC, the JVM performs reachability analysis on objects in Eden, copies surviving objects to the to survivor space using the mark‑copy algorithm, clears Eden, and then swaps the roles of the two survivor spaces for the next cycle.

This process repeats as objects are allocated, ensuring that short‑lived objects are reclaimed quickly and long‑lived objects are eventually promoted to the old generation.

Javamemory managementgarbage collectionReference CountingGenerational GCmark-sweep
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.