Mastering JDK8 Garbage Collection: Visual Cheat Sheet and Tuning Guide
This article presents a comprehensive overview of JDK8's garbage collection mechanisms, detailing memory regions, available collectors, key tuning parameters, thread settings, and practical commands, complemented by eight illustrative diagrams and a downloadable PDF cheat sheet for quick reference.
Overview
JVM has ~1853 options, 680 documented in JDK 8. Most performance impact comes from GC‑related flags.
Memory layout of generational collectors
JVM memory = heap + non‑heap. Heap contains Young Generation (Eden, Survivor) and Old Generation. Non‑heap includes thread stacks, code cache, NIO direct buffers, and Metaspace (native memory).
GC algorithms in JDK 8 (HotSpot)
Parallel GC (throughput‑oriented)
Parallel Scavenge (young‑gen, throughput‑oriented)
Concurrent Mark‑Sweep (CMS)
Garbage‑First (G1, default from JDK 9)
Parallel Scavenge tuning
Two main flags: -XX:MaxGCPauseMillis=<ms> – target maximum pause time. -XX:GCTimeRatio=<ratio> – desired throughput (default 99 %).
Enabling GC logging
For JDK 9+ use: -Xlog:gc* For JDK 8 use the legacy options:
-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStampsThese flags produce timestamps, heap size before/after collection, and pause duration.
Heap and non‑heap size parameters
-Xms<size>– initial heap size. -Xmx<size> – maximum heap size. -XX:MaxMetaspaceSize=<size> – limit Metaspace growth. -XX:NewSize / -XX:MaxNewSize – control Young Generation size. -XX:SurvivorRatio – ratio between Eden and Survivor spaces.
Thread‑Local Allocation Buffers (TLAB)
TLAB allows a thread to allocate objects from a private buffer without synchronization, improving allocation speed. Relevant flags: -XX:+UseTLAB (enabled by default). -XX:TLABSize=<size> – size of each TLAB. -XX:TLABRefill=<size> – refill size when a TLAB is exhausted.
Common GC flags
-XX:InitiatingHeapOccupancyPercent=<percent>– trigger concurrent cycles for G1/CMS. -XX:ParallelGCThreads=<N> – number of threads used by parallel collectors. -XX:ConcGCThreads=<N> – threads for concurrent phases (G1, CMS).
Young‑generation tenuring
Objects that survive a configurable number of young‑gen collections are promoted to the old generation. Control with:
-XX:MaxTenuringThreshold=<N>CMS (Concurrent Mark‑Sweep)
CMS is a low‑pause collector but requires many tuning options (≈100). Frequently used flags include:
-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=<percent> -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSConcurrentMTEnabledNote: CMS was deprecated in JDK 9 and removed in later releases.
G1 collector
G1 partitions the heap into equal‑sized regions (1 MB–32 MB, up to 2048 regions). Regions are grouped logically into Eden, Survivor, and Old sets.
Key tuning parameters:
-XX:+UseG1GC -XX:MaxGCPauseMillis=<ms>– target pause. -XX:G1HeapRegionSize=<size> – region size. -XX:InitiatingHeapOccupancyPercent=<percent> – start concurrent marking. -XX:G1ReservePercent=<percent> – reserve free space.
Reference PDF
A PDF cheat‑sheet that consolidates the above flags is available at:
https://github.com/ddean2009/www.flydean.com/blob/master/cheatSheet/JDK8GC-cheatsheet.pdf
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.
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.
