SoFunction
Updated on 2025-04-18

Share common parameter settings for JVM in production environment

1. Basic memory settings

1.1 Xms and Xmx

  • -Xms: Set the initial heap memory size when JVM starts.
  • -Xmx: Set the maximum heap memory size allowed by the JVM.

suggestion

  • Try to keep​-Xms​and​-Xmx​Set to the same value to avoid the performance overhead of dynamic expansion/reducing heap memory at runtime.
  • According to the actual memory of the server, it is reasonably allocated to the JVM. For example, if the server has 32GB of memory, you can consider setting up​​-Xms 8g -Xmx 8g​Or adjust appropriately according to application needs.

1.2 The ratio of the new generation to the old generation

  • -XX:NewRatio=: Set the ratio of the new generation to the old generation. For example, set​-XX:NewRatio=3​​ means that the old age is three times that of the new generation.
  • -XX:NewSize= and **-XX:MaxNewSize**=: directly set the minimum and maximum sizes of the new generation.

suggestion

  • For most applications, try​​-XX:NewRatio=2​​or​-XX:NewRatio=3​​。
  • If the life cycle of the application object is short, the proportion of the new generation can be appropriately increased.

2. Garbage collector selection

2.1 G1 garbage collector

  • -XX:+UseG1GC: Enable G1 garbage collector.
  • -XX:MaxGCPauseMillis=: Set the maximum pause time target of G1 garbage collector.

suggestion

  • G1 is suitable for large memory applications and can effectively reduce garbage collection pause time.
  • Can be passed​-XX:MaxGCPauseMillis=200​​To set the maximum pause time to 200 milliseconds.

2.2 ZGC garbage collector

  • -XX:+UseZGC: Enable ZGC garbage collector.
  • -XX:ConcGCThreads=: Set the number of concurrent garbage collection threads.

suggestion

  • ZGC is suitable for applications that require extremely low pause times and consumes high CPU resources.
  • Can be passed​-XX:ConcGCThreads=4​​To set the number of concurrent garbage collection threads.

3. Other common parameters

3.1 Thread stack size

  • -Xss: Set the stack size of each thread.

suggestion

  • By default, the thread stack size is 1MB, which can be adjusted according to the number of threads applied and actual needs. For example,​-Xss512k​​。

3.2 Class loading

  • -XX:+UseClassDataSharing: Enable class data sharing.
  • -XX:SharedArchiveFile=: Specify the path to the shared archive file.

suggestion

  • Enabling class data sharing can reduce JVM startup time and memory usage.
  • Can be passed​-XX:SharedArchiveFile=/path/to/shared/​​To specify shared archive files.

3.3 Logging

  • -Xloggc:: Specify the path to the GC log file.
  • -XX:+PrintGCDetails: Print detailed GC log information.
  • -XX:+PrintGCDateStamps: Include date timestamps in GC logs.

suggestion

  • Turning on GC logs helps monitor and analyze JVM garbage collection behavior.
  • Can be passed​-Xloggc:/var/log/jvm/ -XX:+PrintGCDetails -XX:+PrintGCDateStamps​​To configure.

4. Monitoring and tuning

4.1 Monitoring with JMX

  • -: Enable JMX remote management.
  • -=: Set the JMX listening port.
  • -=false: Disable JMX authentication (not recommended in production environment).
  • -=false: Disable SSL (not recommended in production environment).

suggestion

  • Enabling JMX monitoring can help monitor the running status of the JVM in real time.
  • For example,​- -=9010 -=false -=false​​。

4.2 Using VisualVM

  • VisualVM: A graphical tool that can connect to local or remote JVM instances for performance monitoring and troubleshooting.

suggestion

  • Install and use VisualVM for regular performance checks and troubleshooting.

Reasonable JVM parameter settings can significantly improve the performance and stability of Java applications. The above suggestions are for reference only. The specific parameter settings should be adjusted according to the actual needs of the application and the server resource situation. Suggestions for setting JVM parameters in production environments include basic memory settings, garbage collector selection, other common parameters, and monitoring and tuning methods. In a production environment, rational configuration of JVM (Java virtual machine) parameters is crucial to improve application performance, stability and response time. The following are some common JVM parameter settings and their application scenarios. Configure a Spring Boot-based application and deploy it on a Linux server.

Common JVM parameter settings and their application scenarios

1. Heap memory settings

Heap memory is one of the most important parts of the JVM, used to store object instances. Reasonable heap memory settings can avoid frequent garbage collection and memory overflow.

-Xms2g -Xmx2g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m
  • ​-Xms2g​​: The initial heap memory size is set to 2GB.
  • ​-Xmx2g​​: The maximum heap memory size is set to 2GB.
  • ​-XX:MetaspaceSize=256m​​: The initial size of the Metaspace is set to 256MB.
  • ​-XX:MaxMetaspaceSize=512m​​: The maximum size of Metaspace is set to 512MB.

2. Garbage collector selection

Different garbage collectors are suitable for different application scenarios. For example, the G1 garbage collector is suitable for scenarios with large memory and low latency requirements.

-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=35
  • ​-XX:+UseG1GC​​: Use the G1 garbage collector.
  • ​-XX:MaxGCPauseMillis=200​​: Set the maximum pause time for garbage collection to 200 milliseconds.
  • ​-XX:InitiatingHeapOccupancyPercent=35​​: Start hybrid garbage collection when the heap memory usage reaches 35%.

3. Number of concurrent threads

Setting the number of concurrent threads can optimize the performance of multi-core processors.

-XX:ParallelGCThreads=8 -XX:ConcGCThreads=4
  • ​-XX:ParallelGCThreads=8​​: Set the number of parallel garbage collection threads to 8.
  • ​-XX:ConcGCThreads=4​​: Set the number of concurrent garbage collection threads to 4.

4. Logging

Enabling detailed garbage collection logs can help monitor and tune JVM performance.

-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/var/log/app/
  • ​-XX:+PrintGCDetails​​: Print detailed garbage collection information.
  • ​-XX:+PrintGCDateStamps​​: Include timestamps in the garbage collection log.
  • ​-Xloggc:/var/log/app/​​: Specify the path to the garbage collection log file.

5. Other common parameters

There are some other commonly used JVM parameters that can be adjusted according to specific needs.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/app/ -=true
  • ​-XX:+HeapDumpOnOutOfMemoryError​​: Generate a heap dump file when a memory overflow occurs.
  • ​-XX:HeapDumpPath=/var/log/app/​​: Specify the path to the heap dump file.
  • ​-=true​​: Run the JVM in headless mode, suitable for servers without a graphical interface.

Complete Start Command Example

java -Xms2g -Xmx2g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m \
     -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=35 \
     -XX:ParallelGCThreads=8 -XX:ConcGCThreads=4 \
     -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/var/log/app/ \
     -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/app/ -=true \
     -jar /path/to/your/

Things to note

  • Monitoring and tuning: In a production environment, the performance indicators of the JVM should be monitored regularly and the parameters should be adjusted according to actual conditions.
  • test: Before applying new JVM parameters in a production environment, sufficient testing should be carried out in the test environment.
  • Documentation: Record the settings reasons and expected effects of each parameter for subsequent maintenance and debugging.

Through the above configuration, the performance and stability of the application in the production environment can be effectively improved. It is very important to configure JVM (Java Virtual Machine) parameters in a production environment, because reasonable configuration can significantly improve the performance and stability of the application. The following are some common JVM parameter setting suggestions and explanations, which can be adjusted according to specific application requirements:

Common JVM parameter setting suggestions and explanations

1. Heap memory settings

  • -Xms: Set the initial heap memory size when JVM starts. It is recommended to set this value to the same as the maximum heap memory to avoid the performance overhead caused by dynamic expansion of heap memory at runtime.
  • -Xmx: Set the maximum heap memory size of the JVM. According to the actual memory usage of the application, it is usually recommended not exceeding 80% of the physical memory.

For example:

-Xms2g -Xmx2g

2. Young Generation Settings

  • -Xmn: Set the size of the younger generation. The size of the younger generation will affect the frequency and efficiency of garbage collection.
  • -XX:NewRatio=: Set the ratio between young generation and old generation. For example,​-XX:NewRatio=3​​ means that the younger generation accounts for 1/4 of the total heap memory.
  • -XX:SurvivorRatio=: Set the ratio between the Eden area and the Survivor area. For example,​-XX:SurvivorRatio=8​​ means that the Eden area accounts for 8/10 of the younger generation.

For example:

-Xmn512m -XX:NewRatio=3 -XX:SurvivorRatio=8

3. Garbage collector selection

  • -XX:+UseParallelGC: Use parallel garbage collector, suitable for servers with multi-core CPUs.
  • -XX:+UseConcMarkSweepGC: Use CMS (Concurrent Mark-Sweep) garbage collector, suitable for applications that are sensitive to pause time.
  • -XX:+UseG1GC: Use G1 (Garbage First) garbage collector, suitable for applications with large memory and high requirements for pause time.

For example:

-XX:+UseG1GC

4. Other common parameters

  • -XX:MaxPermSize=: Set the maximum size of PermGen (JDK 7 and below only). JDK 8 and above use Metaspace, which can be used through​-XX:MaxMetaspaceSize=<size>​​ to set it.
  • -XX:MaxDirectMemorySize=: Set the maximum size of direct memory.
  • -XX:+HeapDumpOnOutOfMemoryError: Generate a heap dump file when an OutOfMemoryError occurs, which is convenient for subsequent analysis.
  • -XX:HeapDumpPath=: Specify the save path of the heap dump file.
  • -XX:+PrintGCDetails: Print detailed garbage collection logs.
  • -XX:+PrintGCDateStamps: Add timestamp to the garbage collection log.

For example:

-XX:MaxPermSize=256m -XX:MaxDirectMemorySize=1g -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/jvm_heapdump.hprof -XX:+PrintGCDetails -XX:+PrintGCDateStamps

5. Tuning suggestions

  • Monitoring and tuning: Use tools such as JVisualVM, JConsole or third-party monitoring tools (such as Prometheus + Grafana) to monitor the running status of the JVM and adjust parameters according to the monitoring data.
  • Gradually adjust: Do not adjust multiple parameters at once. You should gradually adjust and observe the effect to ensure that the performance and stability of the application are improved after each adjustment.

Example

Suppose you have an application running on a server with 16GB of memory, you can refer to the following JVM parameter settings:

java -Xms4g -Xmx4g -Xmn1g -XX:NewRatio=3 -XX:SurvivorRatio=8 -XX:+UseG1GC -XX:MaxPermSize=256m -XX:MaxDirectMemorySize=1g -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/jvm_heapdump.hprof -XX:+PrintGCDetails -XX:+PrintGCDateStamps -jar 

The above parameters are for reference only and need to be adjusted according to specific circumstances in actual applications. Hope these suggestions help you!

This is the article that shared about the commonly used parameter setting of JVM in production environment. For more related content of JVM parameter setting in production environment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!