Common JVM Options
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
| Prefix | Meaning | Example |
|---|---|---|
- | Standard, stable across JVMs | -version, -cp, -D |
-X | Non-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
| Option | Purpose |
|---|---|
-Xms512m / -Xmx2g | Initial and maximum heap size |
-XX:MaxRAMPercentage=75 | Maximum heap as a share of the machine's or container's memory |
-Xss512k | Stack size of each platform thread |
-XX:MaxMetaspaceSize=256m | Limit on class metadata |
-XX:+UseG1GC, -XX:+UseZGC, -XX:+UseParallelGC | Choose the garbage collector |
-XX:MaxGCPauseMillis=100 | G1's pause-time goal |
-Xlog:gc*:file=gc.log | GC logging to a file |
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=... | Save a heap dump when memory runs out |
-XX:+ExitOnOutOfMemoryError | Stop the JVM on OOM so a supervisor can restart it |
-XX:+UseCompactObjectHeaders | Smaller object headers (Java 25) |
-XX:StartFlightRecording=... | Start Java Flight Recorder at launch |
-Dname=value | Set a system property |
--enable-preview | Allow 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.
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));
}
}UseG1GC = true
UseZGC = false
UseCompactObjectHeaders = false
HeapDumpOnOutOfMemoryError = false
Max heap is set? true
Max heap matches Runtime.maxMemory()? trueExplanation: 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:
-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
- Easy: Run
JvmOptionswith-XX:+UseZGC -XX:+HeapDumpOnOutOfMemoryErrorand check the changed values. - Harder: Run a program with
-XX:+PrintFlagsFinal -versionand find the default values ofMaxHeapSize,ThreadStackSizeandMaxMetaspaceSize.
Key Takeaways
Was this tutorial helpful?