JVM parameter configuration reference

更新时间:
复制 MD 格式

Configure JVM memory parameters for applications monitored by Application Real-Time Monitoring Service (ARMS). Use these recommended values as a starting point and adjust them based on your stress testing results.

Total memory budget

Total JVM memory must not exceed 80% of the server's physical memory. The remaining 20% is reserved for the operating system, thread stacks, and JVM internals.

Total JVM memory is the sum of four regions:

  • Heap (-Xms, -Xmx, -Xmn) -- stores objects and runs garbage collection. Heap size is primarily influenced by application throughput.

  • Metaspace (-XX:MetaspaceSize, -XX:MaxMetaspaceSize) -- stores class metadata. Metaspace size depends on the number of classes your application loads.

  • Off-heap (direct) memory (-XX:MaxDirectMemorySize) -- used by NIO buffers and third-party libraries. Direct memory size depends on throughput and use of libraries such as NIO and gzip.

  • Code cache (-XX:ReservedCodeCacheSize) -- stores JIT-compiled native code.

Recommended values by server specification

-Xms is set equal to -Xmx to prevent pauses caused by heap resizing at runtime.

JVM parameterDescription1C 2G2C 4G4C 8G8C 16G
-XmsInitial heap size1G2560M4G10G
-XmxMaximum heap size1G2560M4G10G
-XmnYoung generation size500M1200M2G5G
-XssThread stack size (JDK 8 default: 1M)1M1M1M1M
-XX:MetaspaceSizeInitial metaspace size128M256M384M512M
-XX:MaxMetaspaceSizeMaximum metaspace size128M256M384M512M
-XX:MaxDirectMemorySizeMaximum off-heap memory size256M256M1G1G
-XX:ReservedCodeCacheSizeCode cache size64M128M256M256M

Note: These values are recommendations. Validate them through stress testing with your actual workload before deploying to production.

Sizing ratios

The recommended values follow these ratios:

  • Heap (-Xmx): approximately 50--65% of total server memory, leaving room for non-heap regions and the OS.

  • Young generation (-Xmn): approximately 50% of the maximum heap size. If you use the G1 garbage collector, consider omitting -Xmn -- G1 manages young generation sizing automatically.

  • Metaspace: MetaspaceSize is set equal to MaxMetaspaceSize to avoid dynamic resizing overhead. Scale the value based on the number of classes your application loads.

Common misconfigurations

MisconfigurationImpact
-Xmx too high (total JVM memory exceeds physical memory)The OS swaps to disk, causing latency spikes, or terminates the JVM process (OOM killer).
-Xmn too large relative to -XmxInsufficient space for the old generation triggers frequent full garbage collections.
-XX:MaxMetaspaceSize too lowjava.lang.OutOfMemoryError: Metaspace occurs when the application loads many classes (common with Spring or reflection-heavy frameworks).
-Xms lower than -XmxThe JVM resizes the heap at runtime, causing pauses. Set both to the same value.