Java / Java 21 Interview Questions
What are the most important JVM flags for tuning Java 21 application performance?
Understanding key JVM flags distinguishes senior engineers from juniors. Here are the flags that matter most for Java 21 production deployments.
| Flag | Category | Purpose |
|---|---|---|
| -Xms / -Xmx | Memory | Initial / max heap size. Set equal to avoid resizing pauses |
| -XX:+UseZGC | GC | Enable ZGC (Generational by default in Java 21) |
| -XX:MaxGCPauseMillis=N | GC | G1 pause target (best effort) |
| -XX:+UseStringDeduplication | GC | G1: deduplicate identical String objects |
| -Xlog:gc*:file=gc.log | Logging | GC logging to file |
| -XX:+HeapDumpOnOutOfMemoryError | Diagnostics | Auto heap dump on OOM |
| -XX:HeapDumpPath=/path | Diagnostics | Location for heap dump |
| -XX:+ExitOnOutOfMemoryError | Reliability | Exit JVM on OOM instead of limping on |
| -Djdk.virtualThreadScheduler.parallelism=N | Loom | Number of carrier threads for VTs |
| --enable-preview | Language | Enable preview features (e.g., String Templates, unnamed classes) |
| -XX:+TieredCompilation | JIT | Multi-tier JIT (default on — rarely need to disable) |
| --add-opens module/package=ALL-UNNAMED | Modules | Open module package to classpath (for reflection) |
# Production example: Java 21 virtual-thread microservice with ZGC
java \
-Xms512m -Xmx2g \
-XX:+UseZGC \
-Xlog:gc*:file=/var/log/app/gc.log:time,uptime:filecount=5,filesize=20m \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/var/log/app/heap.hprof \
-XX:+ExitOnOutOfMemoryError \
-Djava.util.concurrent.ForkJoinPool.common.parallelism=1 \
-jar myapp.jar
# Carrier thread count for virtual threads (default = number of CPU cores)
# -Djdk.virtualThreadScheduler.parallelism=16
# Diagnose pinning (virtual thread pinned to carrier too long)
# -Djdk.tracePinnedThreads=full
More Related questions...