JVM Flags: Definition & Categorization
JVM flags (or JVM options) are command-line parameters used to configure the behavior of the Java Virtual Machine (JVM). These flags help in optimizing performance, managing memory, tuning garbage collection, debugging, and more.
JVM flags can be broadly categorized into the following types:
1. Standard Options (-X flags)
These options are widely supported and stable across different JVM versions.
Common Examples:
-Xms<size>→ Sets the initial heap size.-Xmx<size>→ Sets the maximum heap size.-Xss<size>→ Sets the thread stack size.-Xmn<size>→ Sets the young generation size.-XshowSettings→ Displays JVM settings at startup.
👉 Usage Example:
java -Xms512m -Xmx2g -jar myapp.jar
2. Non-Standard (-XX flags)
These are advanced JVM options that are subject to change across JVM versions. They allow fine-tuning of memory, garbage collection, and performance.
Common Examples:
- Heap & Memory Management:
-XX:InitialHeapSize=<size>→ Equivalent to-Xms-XX:MaxHeapSize=<size>→ Equivalent to-Xmx-XX:NewSize=<size>→ Sets the initial size of the young generation.-XX:MaxNewSize=<size>→ Sets the maximum young generation size.
- Garbage Collection (GC) Tuning:
-XX:+UseG1GC→ Enables the G1 garbage collector.-XX:+UseParallelGC→ Enables parallel garbage collection.-XX:+UseZGC→ Enables ZGC (low-latency GC for large heaps).-XX:MaxGCPauseMillis=<ms>→ Sets GC pause time target.
- Performance & Compilation:
-XX:+TieredCompilation→ Enables tiered JIT compilation.-XX:+AlwaysPreTouch→ Pre-allocates heap memory at startup.-XX:+UseStringDeduplication→ Reduces memory usage by deduplicating string objects.
- Diagnostics & Debugging:
-XX:+HeapDumpOnOutOfMemoryError→ Dumps heap memory when anOutOfMemoryErroroccurs.-XX:+PrintGCDetails→ Prints detailed GC logs.-XX:+UnlockDiagnosticVMOptions→ Enables access to additional JVM diagnostic options.
Usage example
java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar myapp.jar
3. Experimental Flags (-XX:+UnlockExperimentalVMOptions)
These are options that are not stable and may change in future JVM versions.
Examples:
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC→ Enables the experimental Epsilon (no-op) garbage collector.-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC→ Enables the experimental Shenandoah GC.
👉 Usage Example:
java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -jar myapp.jar
4. Logging & Monitoring Flags (-Xlog and -D flags)
These options help in monitoring JVM performance, logging GC activity, and debugging.
Examples:
-Xlog:gc→ Enables garbage collection logging (Java 9+).-Xlog:gc*:file=gc.log:time,uptime,level→ Logs GC details to a file.-Dcom.sun.management.jmxremote→ Enables remote JMX monitoring.-Djava.security.egd=file:/dev/urandom→ Speeds up secure random number generation.
👉 Usage Example:
java -Xlog:gc -jar myapp.jar
5. Container-Aware Flags (For Docker/Kubernetes)
JVM 10+ introduced options to make JVM memory and CPU usage more efficient in containers.
Examples:
-XX:+UseContainerSupport→ Enables JVM awareness of container limits.-XX:MaxRAMPercentage=75.0→ Limits heap usage to 75% of available container memory.
👉 Usage Example:
java -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -jar myapp.jar
Checking Available JVM Flags
To list all JVM options supported by your JVM version, you can use:
java -XX:+PrintFlagsFinal
or for Java 9+:
java -Xlog:flags
Conclusion
JVM flags provide powerful configuration options, from heap management to GC tuning and monitoring. Depending on your use case (server applications, microservices, high-performance systems), selecting the right flags can greatly improve JVM performance.