Overview of Java Garbage Collection Algorithms and Memory Management
The article explains Java’s automatic memory management, detailing JVM memory regions, object liveness via reachability analysis, allocation strategies, and the main garbage‑collection algorithms—Mark‑Sweep, Mark‑Copy, Mark‑Compact—and compares HotSpot collectors such as Serial, Parallel, CMS and G1, guiding developers to choose the best fit for their workloads.
Modern high‑level programming languages manage memory either automatically or manually. Java uses an automatic memory management system that includes a memory allocator and a garbage collector (GC). This article, originally written by a Tencent backend engineer, explains the structure of the JVM memory, the principles of garbage collection, and compares Java GC algorithms.
JVM Memory Regions – The JVM runtime memory is divided into several areas: Program Counter, Java Virtual Machine Stack, Native Method Stack, Java Heap, and Method Area (now Metaspace). The heap is the main region managed by the GC and is further split into Young Generation (Eden, From Survivor, To Survivor) and Old Generation.
Object Liveness Determination – Two main techniques are used: reference counting and reachability analysis. Java (and Go) employ reachability analysis using GC Roots. Objects unreachable from any GC Root are considered dead after a two‑step marking process that may invoke finalize if needed.
GC Roots include references from the JVM stack, static fields, string pool, JNI handles, internal VM references, synchronized locks, JMX beans, etc.
Allocation Strategies in the Java Heap – Objects are first allocated in Eden; large objects may be allocated directly in the Old Generation; long‑living objects are promoted based on the -XX:MaxTenuringThreshold parameter. Space allocation guarantees are checked before a Minor GC, and the HandlePromotionFailure flag determines whether a risky Minor GC or a Full GC is performed.
Garbage Collection Algorithms
1. Mark‑Sweep – Marks live objects then sweeps away unmarked ones. Suitable for old generation with many live objects but can cause fragmentation.
2. Mark‑Copy – Copies live objects from one semi‑space to another, eliminating fragmentation. Efficient for young generation where most objects die quickly.
3. Mark‑Compact – After marking, compacts live objects to one end of the heap, then clears the rest. Works well for old generation with high survival rates.
4. Generational Collection – Combines the above algorithms: young generation typically uses Mark‑Copy, while old generation uses Mark‑Sweep or Mark‑Compact.
Garbage Collectors in HotSpot
• Serial – Single‑threaded, uses Mark‑Copy for young and Mark‑Compact for old generation.
• ParNew – Multithreaded version of Serial.
• Parallel Scavenge – Multithreaded, focuses on throughput, uses Mark‑Copy for young generation.
• SerialOld – Serial collector for old generation (Mark‑Compact).
• Parallel Old – Multithreaded old‑generation collector.
• CMS – Concurrent Mark‑Sweep, low pause times, suitable for latency‑sensitive services.
• G1 – Server‑oriented, region‑based collector with predictable pause times, uses Mark‑Copy.
Choosing a collector depends on the application’s performance goals, pause‑time requirements, and hardware resources.
Reference‑Counting Example (illustrating why Java does not use this algorithm)
public class ReferenceCountingGc {
Object instance = null;
public static void main(String[] args) {
ReferenceCountingGc objA = new ReferenceCountingGc();
ReferenceCountingGc objB = new ReferenceCountingGc();
objA.instance = objB;
objB.instance = objA;
objA = null;
objB = null;
}
}This code creates a cyclic reference between objA and objB . Because their reference counts never reach zero, a pure reference‑counting collector would leak memory, which is why Java relies on reachability analysis.
The article concludes that there is no universally best collector; developers should select the one that best fits their workload and environment.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.