Memory tuning based on JDK8

Tuning of memory structure based on JDK8

​ To perform memory tuning, we must first have a clear understanding of its memory structure

JDK8 memory structure

​ The memory structure of JDK8 mainly includes: program counter, virtual machine stack, local method stack, heap, meta space

​ The heap is divided into: the old generation, the young generation, and the young generation is divided into the Eden area and the Survivor area

Click for details

​ The memory tuning of JDK8 mainly focuses on the heap and meta space. The commonly used JVM parameters are as follows

-server

​ The server mode of JVM, the performance can be very good in the multi-CPU server. Since the 64-bit version of JDK only supports server mode, the option is implicit in this case

-Xmx

​ Specifies the maximum amount of memory allocated by the heap, which is equivalent to -XX: MaxHeapSize. The default unit is byte, which must be a multiple of 1024. E.g

-Xmx1G
-Xmx1024M
-Xmx1048576K
-Xmx1073741824

-Xms

​ Specifies the initial value of the memory allocated by the heap, the default unit is byte. If this initial value is not set, then the initial value will default to the sum of the allocated memory of the old generation and the young generation.

​ For deployment in a production environment, -Xms and -Xmx are usually set to the same value

-Xmn

​ Specify the initial value and maximum value of the memory allocated by the young generation of the heap, the default unit is byte

​ The young area of ​​the heap is used to store new objects. Compared with other areas, garbage collection is performed more frequently in this area. If the memory of the young generation is too small, the garbage will be lost if it is executed multiple times. If it is too large, it will take too long to perform a complete garbage collection.

​ It is generally recommended to keep the size of the young generation at 50%-25% of the entire heap size

​ In addition to using the -Xmn option to set the initial value and maximum value of the young generation, you can also use -XX:NEwSize to set the initial value of the young generation, -XX:MaxnewSize to set the maximum value of the young generation

-XX:NewRatio

​ Specify the ratio of the space size of the old generation and the young generation, the default is 2.

In addition, the priority of the young generation allocation memory settings is as follows

  • High priority: -XX:NewSize -XX:MaxNewSize
  • Medium priority: -Xmn
  • Low priority: -XX:NewRatio

-XX:SurvivorRatio

​ Specify the ratio of Eden to the size of a Survivor area. The default is 8

-XX:MetaspaceSize

​ Specify the threshold of the memory size that the metaspace triggers garbage collection for the first time. When the memory usage of the meta space continues to increase, until this threshold is reached, a garbage collection is triggered. So appropriately increasing this threshold will reduce the number of garbage collections. The default value depends on the platform, generally about 20.8MB

-XX:MaxMetaspaceSize

​ Specify the maximum amount of memory allocated by the metaspace. There is no limit by default. It depends on the amount of available memory in the system. In theory, it can occupy the entire system memory.

​ So you need to set its size appropriately

Memory tuning practice

​ Set the heap memory space as large as possible to reduce the number of memory recycling. When the available memory on the server is still 12GB, you can first specify that the maximum and initial values ​​of memory allocated by the heap are both 8GB. Under normal circumstances, the memory size of the young generation needs to be 50%-25% of the entire heap size, so specify the memory size of the young generation as 3GB. Then set the space size ratio of the Eden area and a Survivor area to 4. The threshold of the memory size at which the metaspace triggers garbage collection for the first time is set to 256MB, which is generally sufficient for use. The maximum memory allocated by the meta space is set to 512MB, in order to avoid taking up a lot of memory in extreme situations. In addition, you need to explicitly specify that the JVM is started in server mode.

​ After the memory tuning parameters are basically determined, use it to start a jar file named one-more-study-0.0.1-SNAPSHOT.jar:

java -server -Xmx8G -Xms8G -Xmn3G -XX:SurvivorRatio=4 -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=512M -jar one-more-study-0.0.1-SNAPSHOT.jar

​ Then use the jmap -heap command to view the memory configuration and usage of the corresponding Java process.

Guess you like

Origin blog.csdn.net/issunmingzhi/article/details/108117154