Mastering JVM Memory: Layout, GC Algorithms, and Tuning Tips
This comprehensive guide explains the JVM memory layout, class‑loading and object‑creation process, the parent‑delegation model, various garbage‑collection algorithms and collectors, GC roots, triggers for YGC and Full GC, and practical tuning parameters to improve Java application performance.
JVM Memory Layout
The JVM consists of several memory areas: the Heap (shared, divided into Young and Old generations; Young further split into Eden, S0, S1 with default 8:1:1 ratio), the Stack (thread‑private, containing frames with local variable tables, operand stacks, dynamic linking, and return addresses), Metadata (class information and runtime constant pool, moved to the heap after Java 7), the Native Method Stack (for native code), and the Program Counter (thread‑private pointer to the current bytecode).
Object Creation Process
When the new keyword is encountered, the JVM checks whether the class is loaded; if not, it performs class loading, verification, preparation (default values for static fields), resolution (replace symbolic references with direct references), and initialization (execute static blocks, initializing parent classes first). After loading, the JVM allocates memory, sets default values for instance fields, fills object header (hash code, GC age, metadata), and finally runs the constructor.
Parent Delegation Model
Class loaders are organized hierarchically: Bootstrap ClassLoader (loads JARs from JAVA_HOME/lib), Extension ClassLoader (loads from JAVA_HOME/lib/ext), Application ClassLoader (loads classes from the application classpath), and optional User‑defined ClassLoaders. Loading a class first queries the parent; if the parent cannot load it, the request proceeds downwards.
Garbage‑Collection Algorithms
Mark‑Sweep
Marks reachable objects by traversing GC roots, then sweeps away all marked objects; this approach can cause memory fragmentation.
Copying Algorithm
Divides memory into two equal regions; live objects are copied from the used region to the other, then the former is cleared, reducing fragmentation but halving usable space.
Mark‑Compact
Marks live objects in the old generation, compacts them toward one end, and frees the remaining space, avoiding fragmentation without copying.
GC Roots
Objects reachable from the following are considered GC roots: references from stack frames, static variables and constant pool entries, and native method stacks.
Garbage Collectors for Young and Old Generations
Young generation collectors: Serial, ParNew, Parallel Scavenge. Old generation collectors: Serial Old, Parallel Old, CMS (Concurrent Mark‑Sweep), and G1 (Garbage‑First, default since JDK 9, no longer separates young/old generations).
G1 Collector Principle
G1 partitions the heap into multiple regions (1‑32 MB each). Large objects become Humongous regions. During GC, G1 prioritizes regions with the highest reclamation benefit within a user‑specified pause‑time target. The four phases are Initial Mark (STW), Concurrent Mark, Final Mark (STW), and Region‑Based Evacuation (STW).
YGC and Full GC Triggers
A Young GC (YGC) occurs when Eden cannot satisfy a new allocation; surviving objects are moved to Survivor spaces, and if Survivor space is full they are promoted to the old generation. If the old generation also lacks space, a Full GC (FGC) is triggered, potentially leading to OOM.
Diagnosing Frequent Full GC
Use jstat -gcutil or inspect gc.log to view GC activity.
Dump heap with jmap -dump:format=b,file=dumpfile pid and analyze with Eclipse Memory Analyzer.
Identify high‑CPU threads via top -p pid -H, map thread IDs to stack traces with jstack pid, and investigate the corresponding code.
JVM Tuning Tips
Enable detailed GC logging (e.g., -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log).
Set -Xms and -Xmx to the same value for a fixed‑size heap.
Use -XX:+HeapDumpOnOutOfMemoryError to capture heap dumps on OOM.
Configure young generation size ( -Xmn) to about 1/4‑1/3 of the total heap.
Disable explicit GC calls with -XX:+DisableExplicitGC.
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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
