JVM Garbage Collection & Tuning Options Cheatsheet
Tuning JVM performance, memory sizes, Garbage Collection strategies, and diagnostic logging.
Interactive Skill Mastery
Mark commands as learned to build your customized reference tracker. Retained locally in this browser.
Memory Allocation
When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -Xms2g -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -Xmx8g -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:MaxMetaspaceSize=512m -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:ActiveProcessorCount=4 -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.Garbage Collection
When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+UseG1GC -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+UseZGC -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:MaxGCPauseMillis=200 -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:InitiatingHeapOccupancyPercent=45 -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+UseStringDeduplication -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.Diagnostics
When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+HeapDumpOnOutOfMemoryError -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:HeapDumpPath=/var/log/dumps -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -Xlog:gc*,gc+phases=debug:file=/var/log/gc.log -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+PrintFlagsFinal -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.Debugging
When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.JIT Compiler
When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:TieredStopAtLevel=1 -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.When to Use
When setting up, debugging, or tuning Java Virtual Machine configurations for optimal memory utilization and efficient garbage collection.
Common Mistakes
Setting the initial heap size (`-Xms`) larger than the maximum heap size (`-Xmx`), which prevents the JVM from booting.
Shortcut / Pro-Tip
Always log GC details and generate automatic heap dumps on OutOfMemory errors using diagnostics flags.Example
java -XX:CompileThreshold=10000 -jar app.jarOutput Example
JVM booted successfully with the specified tuning/allocation parameters.JVM Options Best Practices
1Align Initial (-Xms) and Max (-Xmx) Heap size
Configure initial heap size (-Xms) to be identical to maximum heap size (-Xmx) inside server environments. This avoids JVM performance pauses caused by dynamic heap resizing.
2Pick GCs based on Latency Goals
Utilize G1GC (-XX:+UseG1GC) for standard applications with heap sizes above 4GB. Switch to ZGC (-XX:+UseZGC) or Shenandoah for sub-millisecond maximum pauses.
3Configure Automatic Diagnostic Dumps
Always configure -XX:+HeapDumpOnOutOfMemoryError and designate a physical storage volume directory path to enable retrospective post-mortem debugging.
4Explicitly Set Processor Counts in Containers
Override core container limits using -XX:ActiveProcessorCount to prevent the JVM from miscalculating active threads, avoiding CPU starvation.
5Harness String Deduplication to save memory
Enable string deduplication (-XX:+UseStringDeduplication) with G1GC to merge identical character arrays, freeing up to 10% of total JVM heap space.
Common JVM Options Errors & Solutions
OutOfMemoryError: Metaspace limit reached
Your application loaded more dynamic classes than allowed. Increase limit using -XX:MaxMetaspaceSize=512m or verify classloader leaks.
Error: Could not create the Java Virtual Machine on startup
You set invalid memory configurations, such as setting -Xms larger than -Xmx, or requested a heap size larger than your host system's physical RAM.
OutOfMemoryError: GC overhead limit exceeded
The JVM spent more than 98% of its active execution time performing garbage collection to retrieve less than 2% of the heap. Profile your heap for severe memory leaks.
JVM container process suddenly terminated with exit code 137
The host Linux kernel's Out-Of-Memory (OOM) Killer terminated your container because physical memory limits were breached. Set -Xmx to occupy at most 70-80% of the total container memory space.
Failed to bind debugging socket on port 5005
Another application is utilizing port 5005, or remote firewall rules block binding. Verify host ports and configure address=*:5005 for remote access.
Common JVM Options Interview Questions
Q1What is the difference between -Xms and -Xmx in JVM tuning?
-Xms sets the initial size of the physical memory heap allocated by the OS to the JVM on boot. -Xmx defines the absolute maximum limit the heap is permitted to grow. Matching them avoids runtime resizing overhead.
Q2What is Metaspace and how does it differ from the PermGen space?
PermGen was part of the heap removed in Java 8. Metaspace stores runtime class metadata and is allocated from native memory rather than Java heap. It grows automatically unless restricted by -XX:MaxMetaspaceSize.
Q3When should you prefer the ZGC (Z Garbage Collector) over G1GC?
ZGC should be utilized when application latency and ultra-low pause durations (sub-millisecond) are critical, and you have massive heap allocations (up to terabytes). G1GC is more suitable for general-purpose workloads where brief pause spikes are acceptable.
Q4What does the option -XX:TieredStopAtLevel=1 do?
It disables the heavy C2 compiler optimizations and restricts JIT compilation to Tier 1 (C1 compiler without profiling). This significantly reduces JVM startup times, making it ideal for fast container bootups or local development.
Q5How can you debug a running JVM application that is hanging or slow in production?
You can trigger thread dumps using the 'jstack' tool or send an OS signal (kill -3). To analyze physical object counts, capture a heap dump using 'jmap' or configure -XX:+HeapDumpOnOutOfMemoryError to inspect class memory allocations in tools like Eclipse MAT.
Related Resources
REST API Tester
Test API routes and endpoints directly in your browser with full request controls.
JSON Formatter & Validator
Beautify, validate, and minify JSON structures instantly.
Best Free Online Developer Tools
An expert review of must-have online utilities for developers.
Git Cheatsheet
Essential command reference for local and remote version control repositories.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com