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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
