JDK 8.x microservice startup JVM parameter tuning practice

1.1 Configure JVM startup parameters

server configuration

hardware resource
Memory 6Gi
CPU 4 Nuclear
APP_PARAM="-Xmx2g -Xms2g -Xmn768m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Xss256k -XX:+UseG1GC -XX:+AlwaysPreTouch -XX:-ResizePLAB -XX:+ParallelRefProcEnabled -XX:+ExplicitGCInvokesConcurrent -XX:MaxGCPauseMillis=200  -XX:ParallelGCThreads=4 -XX:ConcGCThreads=2"

1.2 Explanation

This is a variable APP_PARAM that contains several Java Virtual Machine (JVM) parameters, which are used to configure various options of the JVM when starting a Java program. Let's explain the meaning of these parameters one by one:

JVM parameters explain
-Xmx2g Set the maximum heap memory of the JVM to 2GB. This is the maximum amount of memory that the Java heap can use.
-Xms2g Set the initial heap memory of the JVM to 2GB. This is the initial memory size of the Java heap, which will be allocated to the heap when the JVM starts.
-Xmn768m Set the JVM's initial young generation size to 768MB. The young generation is the area in the Java heap used to store newly created objects.
-XX:MetaspaceSize=256m Set the initial size of Metaspace to 256MB. Metaspace is used to store metadata of classes.
-XX:MaxMetaspaceSize=256m Set the maximum size of Metaspace to 256MB. When the Metaspace reaches this size, the JVM will trigger garbage collection to reclaim class metadata that is no longer used.
-Xss256k Set the stack size of each thread to 256KB. This determines how much memory each thread can use.
-XX:+UseG1GC Specifies to use the G1 (Garbage-First) garbage collector. G1 is a modern garbage collector suitable for applications with large memory and multi-core processors.
-XX:+AlwaysPreTouch Specifies that all pages of the heap are pre-allocated and populated when the JVM starts, to avoid delays on subsequent uses.
-XX:-ResizePLAB Disable adaptive Parallel Lab (PLAB) resizing. PLAB is a technique used in the G1 collector to optimize object allocation.
-XX:+ParallelRefProcEnabled Enable parallel reference processing. This allows the G1 collector to use parallel processing when handling references.
-XX:+ExplicitGCInvokesConcurrent Allows garbage collection to be performed in parallel with concurrent marking cycles when System.gc() is explicitly called.
-XX:MaxGCPauseMillis=200 Set the desired maximum GC pause time to 200 milliseconds. The G1 collector will try to keep GC pause times within this range.
-XX:ParallelGCThreads=4 Set the number of threads for parallel garbage collection to 4. This determines the number of threads used when doing parallel garbage collection.
-XX:ConcGCThreads=2 Set the number of concurrent garbage collection threads to 2. This determines the number of threads used during concurrent garbage collection.
  • These JVM parameters can be adjusted according to the specific application and hardware environment to optimize the performance and memory usage of Java programs.
  • Note that some parameters may have different effects in different Java versions or different JVM implementations, so testing and tuning is recommended on a case-by-case basis.

1.3 JVM parameter optimization ideas

Optimizing JVM parameters is a complex process that needs to be adjusted according to specific applications and hardware environments.

Here are some suggested optimizations:

1.3.1 Adjust the heap memory size

Consider appropriately increasing the maximum memory -Xmxand initial memory of the heap according to the actual memory requirements of the application -Xms. But don't over-allocate, and avoid too large a heap that leads to frequent garbage collections.

1.3.2 Young generation size

-Xmn sets the young generation initial size. Moderately adjust the young generation size according to the application's object creation frequency and heap size. A smaller young generation may result in more frequent garbage collections, and a larger young generation may result in less frequent garbage collections.

1.3.3 Metaspace size

-XX:MetaspaceSizeand -XX:MaxMetaspaceSizeset the initial and maximum size of the Metaspace. Moderately increase the Metaspace size according to the class loading requirements of the application.

1.3.4 Stack size

-XssSets the stack size for each thread. Adjust the stack size appropriately according to the thread requirements of the application. An excessively large stack size may cause the number of threads to be limited by available memory.

1.3.5 Garbage collector selection

-XX:+UseG1GCUsing the G1 garbage collector is a good choice, especially in a large memory and multi-core processor environment.

1.3.6 Garbage Collection Parameters

Adjust the parameters of the garbage collector according to the actual situation, such as -XX:MaxGCPauseMillis, , -XX:ParallelGCThreadsand -XX:ConcGCThreads. The adjustment of these parameters requires performance testing and optimization in actual application scenarios.

1.3.7 Pre-allocated memory

-XX:+AlwaysPreTouch pre-allocates heap memory when the JVM starts, avoiding delays caused by subsequent memory allocations.

1.3.8 Disable ResizePLAB

-XX:-ResizePLABParallel Lab (PLAB) sizing can be disabled.


  • Optimizing JVM parameters requires comprehensive consideration of application performance requirements, hardware environment, and available memory resources.
  • It is recommended to conduct testing and performance evaluation in the production environment, and gradually adjust the parameters to achieve the best performance and memory utilization.
  • At the same time, be careful not to over-optimize and avoid introducing new problems due to excessive parameter adjustments.

2. Commonly used JVM parameters

insert image description here

Guess you like

Origin blog.csdn.net/hadues/article/details/132018430