Summary:
JVM parameter tuning is the allocation and use of various kinds of memory during the startup process of Java projects. Reasonable setting of various parameters according to the needs of the project requires a certain understanding of THE JVM memory model and garbage collection mechanism. Keep in mind that JVM parameters are numerous, and many JVMS have enabled parameters that are sufficient by default for your project, so don’t drastically change them all for tuning purposes.
The JVM memory model and garbage collection mechanism are posted here, and you need to understand the meaning of various parameters to tune effectively. Before we start, I’ll post a more comprehensive example of tuning parameters that I personally summarized. The server has 6GB of memory, and I’ll explain each parameter in detail below:
java -Xms4g -Xmx4g -Xss256k -XX:NewSize=1g -XX:MaxNewSize=2g -XX:MetaspaceSize=256m -XX:NewRatio=2 -XX:SurvivorRatio=8
-XX:+DisableExplicitGC -XX:MaxTenuringThreshold=15 -XX:+UseParNewGC -XX:+UseBiasedLocking
-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:ConcGCThreads=8
-XX:ParallelGCThreads=8 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:logs/Gc.log
-jar .\demo.jar
Copy the code
Reference: JVM parameter details
1. JVM parameter syntax
The -xx parameter is called an unstable parameter, so called because setting such a parameter can easily lead to JVM performance differences, making the JVM extremely unstable. Setting these parameters properly will greatly improve the performance and stability of the JVM. Syntax rules for unstable parameters:
1. Boolean parameter values -xx :+<option> '+' indicates that the option is enabled. -xx :-<option> '-' indicates that option 2 is disabled. Numeric type parameter values: -xx :<option>=<number> Sets a numeric type value for the option, which can follow the unit, for example, 'm' or 'm' for megabytes. 'k' or 'k' kilobytes; 'g' or 'g' gigabytes. 32K is the same size as 32768. 3. String parameter values: -xx :<option>=<string> Sets a string value for the option, usually used to specify a file, path, or a list of commands. For example: - XX: HeapDumpPath = / dump. The coreCopy the code
In general, the Springboot project is packaged as a JAR package. You can run the Java -jar.\demo.jar command to start the JAR package on the Tomcat server.
Here is an explanation of the parameters in the example above:
-xMS4G Initial heap size 4g -XMx4G Maximum heap size 4g -xx :NewSize=1g Set the size of the young generation = 1G-xx :MaxNewSize=2g The maximum size of the young generation =2g -xSS256K Stack size of each thread 256K -xx :NewRatio=2 Ratio of young generation (including Eden and two Survivor zones) to old generation (excluding persistent generation), -xx :NewRatio=2 indicates that the ratio of the young generation to the old generation is 1:2, and the young generation accounts for 1/3 of the whole stack. -xx :SurvivorRatio=8 If the value is set to 8, the ratio of two Survivor zones to one Eden zone is 2:8, and one Survivor zone accounts for 1/10 of the entire young generation. -xx :+DisableExplicitGC disable System.gc() -xx :MaxTenuringThreshold=15 Garbage maximum age, If this value is set to a large value, the young generation object will be copied in Survivor zone many times, which increases the survival time of the object in the young generation and the probability of being recycled in the young generation. -xx :+UseParNewGC Sets the parallel collection of the young generation -xx :ConcGCThreads=8 CMS garbage collector parallel threads. The recommended value is the number of CPU cores. -xx :+UseConcMarkSweepGC old CMS memory collection -xx :+UseBiasedLocking Enables an optimized thread lock. Important for high concurrent access, it automatically optimizes for too many requests. For requests of different sizes, The occurrence of blocking, queuing phenomenon, his own optimization. - XX: + CMSParallelRemarkEnabled lower mark pauses - XX: CMSInitiatingOccupancyFraction = 70 use CMS as garbage collection, -xx :+PrintGCDetails output form :[GC [DefNew: 8614 k - > 781 k (9088 k), 0.0123035 secs] 118250 k - > 113543 k (130112 k), 0.0124633 secs] -xx :+PrintGCTimeStamps Prints Gc timestamps -xloggc :logs/ gc. log Logs/gclog. log file for analysis - XX: + HeapDumpOnOutOfMemoryError the heap memory leak, the automatic export heap memory dump snapshot - XX: HeapDumpPath = logs set the export of heap memory snapshot of the store address for logsCopy the code
Reference: JVM performance monitoring and optimization
JVM monitor command:
JPS command to view the JVM process number that the server is running
19008 Jps
3140 RemoteMavenServer36
3604
18108 demo.jar
Copy the code
Jmap – HEAP 18108 – HEAP Displays the summary information about the heap, the algorithm used by GC, the heap configuration, and wise Heap usage.
Using threadlocal object allocation. Concurrent mark-sweep GC # Parallel GC with 4 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 ## MaxHeapFreeRatio = 70 ## MaxHeapFreeRatio specifies the maximum heap free ratio of the JVM (default 70). MaxHeapSize = 2147483648 (2048.0MB) NewSize = 268435456 (256.0MB) MaxNewSize = 268435456 (256.0MB) OldSize = 5439488 (5.1875MB) ## Old generation size: -xx :OldSize=<value>: NewRatio = 2 ## JVM startup parameter -xx :NewRatio= (SurvivorRatio = 8 ## SurvivorRatio) MetaspaceSize = 268435456 MetaspaceSize = 268435456 MetaspaceSize = 268435456 Heap Usage: ## Heap Usage: New Generation (Eden + 1 Survivor Space): ## Cenozoic (Eden + Surviorspace) capacity = 241631232 (230.4375MB) ## Eden used = 77776272 (74.17323303222656MB) ## 163854960 (156.26426696777344MB) ## Remaining capacity Capacity = 214827008 (204.875MB) ## Total Eden space used = 74442288 (70.99369812011719MB) ##Eden space used free = 140384720 (133.8813018798828MB) ## Remaining Eden Space #survior1 capacity = 26804224 (25.5625MB) #survior1 used = 3333984 (3.179534912109375MB) #surviror1 used free = 23470240 (22.382965087890625MB) ## Surviror1 remaining capacity 12.43827838477995% ## survivor2 capacity = 26804224 (25.5625MB) ## survivor2 capacity = 26804224 (25.5625MB) ## Survivor2 capacity = 26804224 (25.5625MB) ## Survivor2 capacity = 26804224 (25.5625MB) ## Survivor2 capacity = 26804224 (25.5625MB) Concurrent mark-sweep generation: ## old age Capacity = 2147483648 (2048.0MB) Used = 42028568 (40.081565856933594MB) Free = 2105455080 (2007.9184341430664MB) 1.9571077078580856%, informsCopy the code
Conclusion:
This article focuses on the JVM tuning parameters in a practical detail, you can follow the steps to learn how to tune.
Mainly do study to share with, if violate rules can contact the author to delete.