Common JVM Options

Naveen AnamaneniUpdated · 4 min read

JVM options are command-line flags that configure the Java Virtual Machine: how much memory it may use, which garbage collector it runs, what it logs, and what it does when something goes wrong. Standard options start with - (such as -version), non-standard ones with -X (such as -Xmx), and advanced ones with -XX:. Most applications need only a handful. In this lesson, you will learn the options worth knowing and how to check which ones a running JVM is using.

Option Families

PrefixMeaningExample
-Standard, stable across JVMs-version, -cp, -D
-XNon-standard but widely supported-Xmx2g, -Xss512k, -Xlog:gc
-XX:Advanced HotSpot options; + turns on, - turns off-XX:+UseZGC, -XX:MaxMetaspaceSize=256m

The Options You Will Use

OptionPurpose
-Xms512m / -Xmx2gInitial and maximum heap size
-XX:MaxRAMPercentage=75Maximum heap as a share of the machine's or container's memory
-Xss512kStack size of each platform thread
-XX:MaxMetaspaceSize=256mLimit on class metadata
-XX:+UseG1GC, -XX:+UseZGC, -XX:+UseParallelGCChoose the garbage collector
-XX:MaxGCPauseMillis=100G1's pause-time goal
-Xlog:gc*:file=gc.logGC logging to a file
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=...Save a heap dump when memory runs out
-XX:+ExitOnOutOfMemoryErrorStop the JVM on OOM so a supervisor can restart it
-XX:+UseCompactObjectHeadersSmaller object headers (Java 25)
-XX:StartFlightRecording=...Start Java Flight Recorder at launch
-Dname=valueSet a system property
--enable-previewAllow preview language features and APIs

Example: Reading Options from Inside the JVM

Let's see an example that reads a few settings from the running JVM.

JvmOptions.java▶ Try it
import com.sun.management.HotSpotDiagnosticMXBean;
import java.lang.management.ManagementFactory;
 
public class JvmOptions {
    public static void main(String[] args) {
        HotSpotDiagnosticMXBean hs =
            ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
        for (String flag : new String[] {"UseG1GC", "UseZGC",
                "UseCompactObjectHeaders", "HeapDumpOnOutOfMemoryError"}) {
            System.out.println(flag + " = " + hs.getVMOption(flag).getValue());
        }
        long maxHeapMb =
            Long.parseLong(hs.getVMOption("MaxHeapSize").getValue()) / 1048576;
        System.out.println("Max heap is set? " + (maxHeapMb > 0));
        System.out.println("Max heap matches Runtime.maxMemory()? "
            + (Math.abs(maxHeapMb - Runtime.getRuntime().maxMemory() / 1048576)
               <= maxHeapMb / 10));
    }
}
Output
UseG1GC = true
UseZGC = false
UseCompactObjectHeaders = false
HeapDumpOnOutOfMemoryError = false
Max heap is set? true
Max heap matches Runtime.maxMemory()? true

Explanation: HotSpotDiagnosticMXBean.getVMOption reads any -XX flag's current value. With no options given, G1 is on, compact headers and heap dumps on OOM are off, and the maximum heap was chosen automatically (by default, a quarter of the machine's memory). From outside a running JVM, jcmd <pid> VM.flags shows the same information:

Terminal output
-XX:InitialHeapSize=134217728
-XX:MaxHeapSize=134217728
-XX:SoftMaxHeapSize=134217728
-XX:+UseG1GC

(Those lines are an excerpt for a JVM started with -Xmx128m, which set the heap to 134,217,728 bytes.)

Containers

Common Mistakes

Copying long lists of old tuning flags from the internet. Many are obsolete or removed; start with defaults and measure.

Setting -Xmx equal to the container's memory limit. Non-heap memory pushes the process over the limit and it gets killed.

Mistyping a -XX flag. The JVM refuses to start with "Unrecognized VM option".

Practice

  1. Easy: Run JvmOptions with -XX:+UseZGC -XX:+HeapDumpOnOutOfMemoryError and check the changed values.
  2. Harder: Run a program with -XX:+PrintFlagsFinal -version and find the default values of MaxHeapSize, ThreadStackSize and MaxMetaspaceSize.

Quick check: JVM options

4 questions
1Which option sets the maximum heap size?
2What does -XX:+UseZGC do?
3Which option is better in containers than a fixed -Xmx?
4Which command shows the flags of a running JVM?

Key Takeaways

Was this tutorial helpful?

Lesson 450 of 482