Skip to main content

πŸ—“οΈ 31082025 1203
πŸ“Ž

jvm_options

🧠 JVM Options Cheatsheet (with Explanations)

A. Heap Memory Management​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-Xmx<size>Sets maximum heap sizeIf RAM < = 16G -> 50% of RAMToo small -> frequent GCs; too large -> OOMLow
-Xmx<size>Same as aboveIf RAM >16G -> 65% of RAMSame as aboveLow
-Xmx<size>Ensures minimum allocationAlways > = 40% of RAMHeap too small -> OOM under loadMedium
-Xmx<size>Prevents starving OS/direct memoryAlways < = 75% of RAMToo large -> OS and NIO buffers starvedMedium
-Xms<size>Sets initial heap sizeSet -Xms = -XmxWithout this, JVM resizes heap dynamically -> pauses & instabilityHigh

B. Direct Memory & Buffer Control​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-XX:MaxDirectMemorySizeAllocates off-heap direct memory (e.g., NIO buffers, Netty)Set = 15% * -XmxToo high -> heap starved; too low -> frequent GCsLow
-XX:MaxDirectMemorySizeMinimum recommended sizeerm > = 256MToo small -> NIO buffers failMedium
-XX:MaxDirectMemorySizeUpper safety caperm < = 25% * -XmxToo large -> less space for heapMedium
--add-opens=java.base/jdk.internal.misc=ALL-UNNAMEDOpens restricted APIs for monitoringRequired for accurate direct memory metricsMissing this -> some monitoring tools breakMedium

C. OutOfMemory (OOM) Troubleshooting​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-XX:+HeapDumpOnOutOfMemoryErrorDumps heap when OOM occursAlways enableNo heap dump -> hard to troubleshootHigh
-XX:HeapDumpPath=Path to store heap dumpsUse persistent storage: Container /hostdata/crash/{svc}.oom.hprof Host /data/crash/{svc}.oom.hprofWrong path -> dump lost after container crashHigh
-XX:ErrorFile=JVM fatal error logsSet persistent paths: Container /hostdata/crash/{svc}.hs_err_pid%p.log Host /data/crash/{svc}.hs_err_pid%p.logWrong path -> JVM crash logs lostHigh

D. Garbage Collection (GC) & Logging​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-Xlog:gc*Enables GC loggingIf logs synced: filecount=1, filesize=200mOversized GC logs -> disk bloat; wrong level -> missing infoHigh
-Xlog:gc*Same as aboveIf logs not synced: filecount=5, filesize=200mNeeded for historical troubleshootingHigh

E. Logging & Encoding​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-Dlog4j2.contextSelectorEnables async loggingSet = org.apache.logging.log4j.core.async.AsyncLoggerContextSelectorWithout it -> sync logging blocks threads under loadHigh
-Dfile.encodingDefault JVM encodingSet = UTF-8Using non-UTF8 -> encoding issues, broken transfersHigh

F. Security & Stability​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-Dfastjson.parser.safeModeSecures Fastjson parserSet = trueVulnerable to remote code execution (Ref)High
-Djava.awt.headlessDisables GUI renderingSet = trueWithout it -> CPU wasted on GUI initHigh
-Dspring.devtools.restart.enabledHot reload toggleSet = false in prodHot reload restarts prod services unexpectedlyHigh
(General)Don’t put secrets in JVM argsUse KMS insteadSecrets exposed in logs / process listsHigh
(General)Uniform JVM configsAll instances must matchInconsistent configs -> stability risksHigh

G. Performance & Profiling​

ParameterPurposeRecommended RuleRisk if MisconfiguredSeverity
-XX:+UseZingMXBeansExposes JVM metricsAlways enableWithout it -> harder debuggingHigh
-XX:+ProfileLiveObjectsEnables object histograms via jmap -histoAlways enableWithout it -> less visibility into leaksHigh
-XX:ProfileLogInPath for JIT profile logsContainer: /app/logs/profile_jit.log Host: /data/logs/{svc}/profile_jit.logWithout it -> ReadyNow JIT warmup optimization disabledMedium
-XX:ProfileLogOutSame as aboveSame path as ProfileLogInSame impact as aboveMedium

βœ… Quick Recommendations​

1. Heap Settings​

  • Start: -Xms = -Xmx
  • Size:
    • < = 16G -> -Xmx = 50% of RAM
    • 16G -> -Xmx = 65% of RAM

2. Direct Memory​

  • Default ~ 15% * -Xmx
  • Keep < = 25% * -Xmx
  • Always >= 256M

3. Logging​

  • Use async logging.
  • Keep GC logs rotated (filesize=200m, filecount=5 if not synced).

4. Security​

  • Always enable -Dfastjson.parser.safeMode=true
  • Avoid secrets in JVM args.

5. Troubleshooting​

  • Enable heap dumps, error logs, and profile logs.
  • Always keep paths in persistent volumes for containers.

References