Visual diagram of JVM memory areas — Heap, Young/Old generation, Metaspace, Stack, PC Register and Code Cache explained.
| Area | Scope | Stores | GC Managed? |
|---|---|---|---|
| Heap — Young Gen | All threads | New objects (Eden + Survivor S0/S1) | Yes — Minor GC |
| Heap — Old Gen (Tenured) | All threads | Long-lived objects promoted from Young Gen | Yes — Major/Full GC |
| Metaspace | All threads | Class metadata, method bytecode, static variables (Java 8+) | Yes — when full |
| Stack | Per thread | Stack frames: local vars, operand stack, method references | No — auto on return |
| PC Register | Per thread | Address of current JVM instruction being executed | No |
| Native Method Stack | Per thread | Frames for native (JNI) method calls | No |
| Code Cache | All threads | JIT-compiled native machine code | Flushed when full |
| Region | Size Default | GC Type | Promotion Rule |
|---|---|---|---|
| Eden | ~80% of Young Gen | Minor GC on fill | Objects survive → S0 or S1 |
| Survivor S0 / S1 | ~10% each of Young Gen | Minor GC | Age threshold reached → Old Gen |
| Old Gen (Tenured) | ~2/3 of total heap | Major / Full GC | Source of OutOfMemoryError |
| Humongous (G1 only) | ≥ 50% of region size | Concurrent GC | Allocated directly in Old Gen |
| Flag | What It Controls | Example |
|---|---|---|
| -Xms | Initial heap size | -Xms512m |
| -Xmx | Maximum heap size | -Xmx2g |
| -Xss | Thread stack size | -Xss512k |
| -XX:MetaspaceSize | Initial Metaspace commit size | -XX:MetaspaceSize=128m |
| -XX:MaxMetaspaceSize | Cap on Metaspace growth | -XX:MaxMetaspaceSize=256m |
| -XX:NewRatio | Old Gen : Young Gen ratio | -XX:NewRatio=2 (2:1 ratio) |
| -XX:SurvivorRatio | Eden : Survivor ratio | -XX:SurvivorRatio=8 (8:1:1) |
| -XX:+UseG1GC | Enable G1 garbage collector | Default from Java 9+ |
| -XX:MaxGCPauseMillis | Target max GC pause (G1) | -XX:MaxGCPauseMillis=200 |
| GC | Algorithm | Pause Type | Best For |
|---|---|---|---|
| Serial GC | Single-threaded mark-sweep-compact | Stop-the-world | Single-core, small heaps |
| Parallel GC | Multi-threaded throughput collector | Stop-the-world | Batch jobs, high throughput |
| G1 GC | Region-based, concurrent marking | Short predictable pauses | Default — balanced latency/throughput |
| ZGC | Concurrent, colored pointers | Sub-millisecond | Large heaps, low latency (Java 15+) |
| Shenandoah | Concurrent evacuation | Sub-millisecond | Low-latency, Red Hat JDK |
| Error | Cause | Fix |
|---|---|---|
| java.lang.OutOfMemoryError: Java heap space | Old Gen full — objects not GC'd | Increase -Xmx, fix memory leaks |
| java.lang.OutOfMemoryError: Metaspace | Too many classes loaded / never unloaded | Increase -XX:MaxMetaspaceSize, check classloaders |
| java.lang.OutOfMemoryError: GC overhead limit exceeded | GC running > 98% of time, recovering < 2% heap | Increase heap, reduce object creation rate |
| java.lang.StackOverflowError | Stack depth exceeded (infinite recursion) | Fix recursion, increase -Xss |
| java.lang.OutOfMemoryError: unable to create native thread | OS limit on threads reached | Reduce thread count, increase OS limits |
| Command / Flag | Purpose | Example |
|---|---|---|
| jstat -gcutil | GC stats every 1s — S0/S1/E/O/M utilisation + GC counts | jstat -gcutil 12345 1000 |
| jstat -gccause | Show last GC cause (e.g. Allocation Failure) | jstat -gccause 12345 |
| -verbose:gc | Print GC events to stdout | java -verbose:gc -jar app.jar |
| -Xlog:gc*:file=gc.log:time,uptime | JVM 9+ unified GC logging to file | Replaces -XX:+PrintGCDetails |
| -XX:+HeapDumpOnOutOfMemoryError | Auto dump heap on OOM | -XX:HeapDumpPath=/tmp/heapdump.hprof |
| -XX:+PrintGCDateStamps | Add timestamps to GC log (Java 8) | Combine with -XX:+PrintGCDetails |
| jcmd | Trigger GC manually (diagnostic only) | jcmd 12345 GC.run |
| jcmd | Show all active JVM flags for running process | jcmd 12345 VM.flags |
| Tool | Type | Best For |
|---|---|---|
| jmap -heap | CLI — heap summary | Quick check of heap usage, GC config |
| jmap -histo | CLI — object histogram | Find which classes consume most heap |
| jmap -dump:live,format=b,file=h.hprof | CLI — heap dump | Capture snapshot for offline analysis |
| VisualVM | GUI profiler | CPU/memory profiling, heap dump analysis, GC monitoring |
| JProfiler / YourKit | Commercial GUI profiler | Production-grade CPU/memory/thread profiling |
| Eclipse MAT | Heap dump analyser | Find memory leaks, dominator tree, retained heap |
| Async-profiler | Low-overhead sampling profiler | CPU flame graphs, allocation profiling without safepoint bias |
| jconsole | JMX GUI | Quick monitoring of heap, threads, MBeans |