Master Java GC: Understand Memory, Parameters, and Algorithms for Interviews
This article explains Java garbage collection fundamentals, detailing JVM memory layout, key -X and -XX parameters, the mechanisms of object survival, and the main GC algorithms—Mark‑Sweep, Copying, Mark‑Compact, and generational collection—providing clear diagrams and interview‑ready insights.
JVM Memory Structure Overview
Java GC is a core interview topic. The JVM heap is divided into three regions: Eden, From Survivor, and To Survivor, with a size ratio of 8:1:1. This design reflects the behavior of garbage collection.
JVM Parameters Explained
-Xms sets the minimum heap size.
-Xmx sets the maximum heap size.
-Xmn sets the initial and maximum size of the young generation (refined by -XX:NewSize and -XX:MaxNewSize).
-XX:NewSize sets the minimum size of the young generation.
-XX:MaxNewSize sets the maximum size of the young generation.
-XX:PermSize sets the minimum size of the permanent generation.
-XX:MaxPermSize sets the maximum size of the permanent generation.
-Xss sets the stack size for each thread.
These parameters correspond to the regions shown in the heap diagram, making them easier to understand.
GC Overview
Garbage Collection (GC) is performed automatically by the JVM. Understanding GC is essential for configuring the above parameters and for troubleshooting memory‑related issues such as leaks or high‑concurrency bottlenecks.
How Objects Survive
Object liveness is determined by two main methods: reference‑counting and reachability analysis. Java uses reachability analysis, starting from GC Roots (system class loader, thread stacks, etc.) and tracing reference chains. Objects not reachable from any GC Root are considered dead.
GC Algorithms
Mark‑Sweep
The Mark‑Sweep algorithm consists of a marking phase, where live objects are identified, followed by a sweeping phase that reclaims the memory of unmarked objects. It is simple but can leave fragmented memory.
Copying
The Copying algorithm divides the young generation into two equal halves. Live objects are copied from the active half to the other half, and the former half is cleared. This avoids fragmentation and provides fast allocation.
Mark‑Compact
Mark‑Compact combines marking with a compaction step: after marking live objects, they are moved to one end of the heap, and the remaining space is reclaimed in a single contiguous block, eliminating fragmentation.
Generational Collection
The heap is logically split into the young generation and the old generation. The young generation, where most objects are short‑lived, uses the Copying algorithm. The old generation, where objects have higher survival rates, uses the Mark‑Compact algorithm.
When the Eden space fills, surviving objects are copied to a Survivor space; if a Survivor space becomes full, objects are promoted to the old generation based on their age.
老年代空间大小=堆空间大小-年轻代大空间大小Summary
By understanding JVM memory layout, key parameters, and the various GC algorithms, you can confidently answer interview questions and tune the JVM for better performance and stability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
