Home Cheat Sheets JVM Memory Model Diagram
📋 CHEAT SHEET

JVM Memory Model Diagram

Visual diagram of JVM memory areas — Heap, Young/Old generation, Metaspace, Stack, PC Register and Code Cache explained.

Memory Areas Overview

AreaScopeStoresGC Managed?
Heap — Young GenAll threadsNew objects (Eden + Survivor S0/S1)Yes — Minor GC
Heap — Old Gen (Tenured)All threadsLong-lived objects promoted from Young GenYes — Major/Full GC
MetaspaceAll threadsClass metadata, method bytecode, static variables (Java 8+)Yes — when full
StackPer threadStack frames: local vars, operand stack, method referencesNo — auto on return
PC RegisterPer threadAddress of current JVM instruction being executedNo
Native Method StackPer threadFrames for native (JNI) method callsNo
Code CacheAll threadsJIT-compiled native machine codeFlushed when full

Heap Generations

RegionSize DefaultGC TypePromotion Rule
Eden~80% of Young GenMinor GC on fillObjects survive → S0 or S1
Survivor S0 / S1~10% each of Young GenMinor GCAge threshold reached → Old Gen
Old Gen (Tenured)~2/3 of total heapMajor / Full GCSource of OutOfMemoryError
Humongous (G1 only)≥ 50% of region sizeConcurrent GCAllocated directly in Old Gen

Key JVM Flags

FlagWhat It ControlsExample
-XmsInitial heap size-Xms512m
-XmxMaximum heap size-Xmx2g
-XssThread stack size-Xss512k
-XX:MetaspaceSizeInitial Metaspace commit size-XX:MetaspaceSize=128m
-XX:MaxMetaspaceSizeCap on Metaspace growth-XX:MaxMetaspaceSize=256m
-XX:NewRatioOld Gen : Young Gen ratio-XX:NewRatio=2 (2:1 ratio)
-XX:SurvivorRatioEden : Survivor ratio-XX:SurvivorRatio=8 (8:1:1)
-XX:+UseG1GCEnable G1 garbage collectorDefault from Java 9+
-XX:MaxGCPauseMillisTarget max GC pause (G1)-XX:MaxGCPauseMillis=200

Garbage Collectors

GCAlgorithmPause TypeBest For
Serial GCSingle-threaded mark-sweep-compactStop-the-worldSingle-core, small heaps
Parallel GCMulti-threaded throughput collectorStop-the-worldBatch jobs, high throughput
G1 GCRegion-based, concurrent markingShort predictable pausesDefault — balanced latency/throughput
ZGCConcurrent, colored pointersSub-millisecondLarge heaps, low latency (Java 15+)
ShenandoahConcurrent evacuationSub-millisecondLow-latency, Red Hat JDK

Common Memory Errors

ErrorCauseFix
java.lang.OutOfMemoryError: Java heap spaceOld Gen full — objects not GC'dIncrease -Xmx, fix memory leaks
java.lang.OutOfMemoryError: MetaspaceToo many classes loaded / never unloadedIncrease -XX:MaxMetaspaceSize, check classloaders
java.lang.OutOfMemoryError: GC overhead limit exceededGC running > 98% of time, recovering < 2% heapIncrease heap, reduce object creation rate
java.lang.StackOverflowErrorStack depth exceeded (infinite recursion)Fix recursion, increase -Xss
java.lang.OutOfMemoryError: unable to create native threadOS limit on threads reachedReduce thread count, increase OS limits

GC Tuning Commands

Command / FlagPurposeExample
jstat -gcutil 1000GC stats every 1s — S0/S1/E/O/M utilisation + GC countsjstat -gcutil 12345 1000
jstat -gccause Show last GC cause (e.g. Allocation Failure)jstat -gccause 12345
-verbose:gcPrint GC events to stdoutjava -verbose:gc -jar app.jar
-Xlog:gc*:file=gc.log:time,uptimeJVM 9+ unified GC logging to fileReplaces -XX:+PrintGCDetails
-XX:+HeapDumpOnOutOfMemoryErrorAuto dump heap on OOM-XX:HeapDumpPath=/tmp/heapdump.hprof
-XX:+PrintGCDateStampsAdd timestamps to GC log (Java 8)Combine with -XX:+PrintGCDetails
jcmd GC.runTrigger GC manually (diagnostic only)jcmd 12345 GC.run
jcmd VM.flagsShow all active JVM flags for running processjcmd 12345 VM.flags

Memory Profiling Tools

ToolTypeBest For
jmap -heap CLI — heap summaryQuick check of heap usage, GC config
jmap -histo CLI — object histogramFind which classes consume most heap
jmap -dump:live,format=b,file=h.hprof CLI — heap dumpCapture snapshot for offline analysis
VisualVMGUI profilerCPU/memory profiling, heap dump analysis, GC monitoring
JProfiler / YourKitCommercial GUI profilerProduction-grade CPU/memory/thread profiling
Eclipse MATHeap dump analyserFind memory leaks, dominator tree, retained heap
Async-profilerLow-overhead sampling profilerCPU flame graphs, allocation profiling without safepoint bias
jconsoleJMX GUIQuick monitoring of heap, threads, MBeans
More Cheat Sheets
Java Collections Cheat SheetJava Streams API Cheat SheetPython Built-in Functions Cheat SheetSQL Joins Cheat SheetDocker Commands Cheat SheetHow HashMap Works InternallyMicroservices Cheat SheetPandas Cheat SheetData Structures & Algorithms Cheat Sheet