Comprehensive JVM Garbage Collection Interview Questions and Answers
This article compiles a series of JVM interview questions covering heap partitioning, garbage‑collection algorithms, collector types, memory coordination, tuning strategies, and practical tooling such as jstat, jmap, and MAT, providing detailed explanations and command‑line examples for each topic.
1. Headline Interview: JVM heap partition from GC perspective
Answer: The Java heap is divided into the old generation and the young generation. The young generation further consists of the Eden space and two Survivor spaces (S0 and S1).
Why split the heap?
Over 90% of objects are short‑lived; placing newly created objects in Eden and long‑lived objects in the old generation improves garbage‑collection performance.
Why further subdivide the young generation?
Eden fills quickly; when it is full, a Minor GC moves surviving objects to Survivor spaces, reducing the frequency of Full GCs. Survivor spaces act as a buffer between Eden and the old generation.
Do Survivor spaces undergo GC?
Yes, but they are reclaimed implicitly during Eden’s GC; the default ratio of Eden:S0:S1 is 8:1:1.
Why not just one Eden and one Survivor?
The copy algorithm uses two Survivor spaces to avoid fragmentation and to ensure that live objects are copied into a contiguous region.
2. Meituan Interview: JVM garbage‑collection algorithms
The article lists seven algorithms:
1. Reachability analysis (mark phase)
Starts from GC roots (stack, static fields, etc.) and marks all reachable objects.
2. Mark‑Sweep (young‑gen)
Stops the world, marks reachable objects, then sweeps unmarked memory, which can cause fragmentation.
3. Copying algorithm (young‑gen)
Copies live objects to a new space, leaving the old space free; avoids fragmentation but requires extra memory.
4. Mark‑Compact (old‑gen)
Marks live objects, then compacts them to one end of the heap, eliminating fragmentation.
5. Generational collection
Uses different algorithms for young (copying) and old (mark‑sweep or mark‑compact) generations.
6. Incremental collection
Interleaves GC work with application threads to reduce pause times, at the cost of higher overall overhead.
7. Region‑based (G1) collection
Divides the heap into equal‑sized regions and collects them independently, allowing predictable pause times.
3. Didi Interview: Types of GC collectors and their pros/cons
Young‑generation collectors
Serial : Single‑threaded, uses copying; pauses all application threads.
ParNew : Multithreaded version of Serial.
ParallelScavenge : Targets overall throughput rather than minimal pause.
Old‑generation collectors
SerialOld : Single‑threaded, uses mark‑compact.
ParallelOld : Multithreaded, also uses mark‑compact.
CMS (Concurrent Mark‑Sweep) : Aims for short pauses; consists of initial mark, concurrent mark, remark, and concurrent sweep phases.
Heap‑wide collector
G1 : Introduced in JDK 7, default in JDK 9; partitions the heap into regions and performs incremental collection.
4. Bytedance Interview: Coordination between JVM memory regions
The runtime data area includes Method Area, Java Heap, Java Stacks, Native Method Stacks, and Program Counter. Class loaders, execution engine, and native libraries reside outside the runtime data area.
How do heap, stack, and method area interact?
Stacks hold references to heap objects; method area stores class metadata referenced by those objects; the program counter links to the current execution point.
Parent‑delegation mechanism
Ensures core Java classes are loaded by the bootstrap class loader, preventing tampering; certain frameworks (JDBC, Tomcat, OSGi) may break this mechanism.
Memory allocation strategies
Free‑list, pointer‑bumping, and CAS‑based allocation are discussed.
Why Metaspace?
Replaces the Permanent Generation to overcome its size limits; however, Metaspace can still suffer fragmentation.
5. Ant Financial Interview: G1 collector overview
G1 divides the heap into equal regions, allowing independent collection and more predictable pause times.
6. Bytedance Interview: CMS pause behavior
CMS performs two short STW pauses (initial mark and remark) to minimize impact while still achieving concurrent collection.
7. JD Interview: JVM tuning methodology
Three aspects: identify whether performance issues stem from code or JVM parameters, monitor and diagnose using tools (jstat, jmap, jvisualvm, MAT), and adjust parameters such as -Xms/-Xmx, -Xmn, and -XX:+DisableExplicitGC.
-XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:gc.log -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\jvm.dump jmap -dump:format=b,file=D:/demo.hprof pid8. Alibaba Interview: When does GC trigger?
Minor GC occurs when Eden is full; Full GC occurs when promotion fails, old‑gen usage exceeds a threshold, Metaspace is exhausted, or System.gc() is invoked.
9. Meituan Interview: Using jstat, jmap, MAT
Commands to locate process IDs, evaluate memory usage, generate heap dumps, and analyze them with MAT are provided.
10. Additional Q&A: Effect of enlarging Eden on Minor GC
Increasing Eden reduces Minor GC frequency but does not necessarily increase each GC’s duration; the impact depends on the proportion of long‑lived objects.
— End —
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.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
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.
