Java & Spring
Updated for 2026

JVM Garbage Collection & Tuning Options Cheatsheet

Tuning JVM performance, memory sizes, Garbage Collection strategies, and diagnostic logging.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 18 Mastered (0%)

Memory Allocation

-Xms2g
BeginnerBasics
Set the initial heap size of the JVM to 2 Gigabytes on startup.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-Xmx8g
BeginnerBasics
Set the maximum limit of the JVM heap size to 8 Gigabytes.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:MaxMetaspaceSize=512m
BeginnerBasics
Restrict the maximum size of Metaspace (where class definitions are stored) to 512 MB.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:ActiveProcessorCount=4
BeginnerBasics
Override auto-detected CPU cores to explicitly limit JVM threads inside virtual containers.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.

Garbage Collection

-XX:+UseG1GC
BeginnerBasics
Activate the G1 (Garbage-First) Garbage Collector (default for server-class machines).

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:+UseZGC
BeginnerBasics
Activate the low-latency Z Garbage Collector (designed for huge heaps and sub-millisecond pauses).

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
BeginnerBasics
Activate Shenandoah ultra-low-pause Garbage Collector to complete major collections concurrently.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:MaxGCPauseMillis=200
BeginnerBasics
Provide a target maximum soft GC pause duration hint to the Garbage Collector.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:InitiatingHeapOccupancyPercent=45
BeginnerBasics
Set the heap threshold ratio that triggers G1 Garbage Collection cycles.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:+UseStringDeduplication
BeginnerBasics
Instruct G1GC to deduplicate identical character arrays in memory automatically.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.

Diagnostics

-XX:+HeapDumpOnOutOfMemoryError
BeginnerBasics
Instruct the JVM to generate a physical heap dump file when an OutOfMemoryError occurs.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:HeapDumpPath=/var/log/dumps
IntermediateDebugging
Specify the destination file directory path for saving OutOfMemoryError heap dumps.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-Xlog:gc*,gc+phases=debug:file=/var/log/gc.log
IntermediateDebugging
Enable comprehensive garbage collection logging with high-fidelity debug phases.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:+PrintFlagsFinal
BeginnerBasics
Print all active JVM configurations and options to standard out on startup for audits.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.

Debugging

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
IntermediateDebugging
Enable remote debugging connections over a specified socket port.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.

JIT Compiler

-XX:TieredStopAtLevel=1
BeginnerBasics
Disable C2 compiler to optimize JVM startup time (ideal for local development or fast serverless containers).

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation
IntermediateDebugging
Track JIT compiler execution pipelines and optimizations inside detailed compiling logs.

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.jar

Output Example

Console / Terminal
JVM booted successfully with the specified tuning/allocation parameters.
-XX:CompileThreshold=10000
BeginnerBasics
Tune the JIT Compiler's method execution frequency before hot methods are optimized to machine code.

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.jar

Output Example

Console / Terminal
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

Error

OutOfMemoryError: Metaspace limit reached

Solution

Your application loaded more dynamic classes than allowed. Increase limit using -XX:MaxMetaspaceSize=512m or verify classloader leaks.

Error

Error: Could not create the Java Virtual Machine on startup

Solution

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.

Error

OutOfMemoryError: GC overhead limit exceeded

Solution

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.

Error

JVM container process suddenly terminated with exit code 137

Solution

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.

Error

Failed to bind debugging socket on port 5005

Solution

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.