This is the 26th day of my participation in the August Text Challenge.More challenges in August

1. Introduction

This article mainly lists the configuration parameters commonly used by the JVM, including heap memory configuration and garbage collector configuration, etc. For JVM parameters, there are too many, we can remember a few commonly used, the rest is just to make an understanding, we need to use when we can remember and find it.

So this article only on some of the development often used in some parameters to do an analysis


2. Parameters related to heap memory

By default, Young = 1/3 heap size, Old = 2/3 heap size; The New generation (Young) is subdivided into Eden and two Survivor regions, which are named from and to respectively to distinguish them. By default, Edem: from: to = 8:1:1

Parameters to specify heap memory:

  • -Xms: Sets the initial heap size. By default, the initial heap size is computer memory size /64
  • -Xmx: Sets the maximum heap size. By default, the maximum heap size is computer memory size /4

For example, set the minimum heap size to 2G and the maximum heap size to 5G. Of course, the units can also be replaced by m (MB) and K (KB). Usually we set these two parameters to the same size to improve performance

-Xms2G -Xmx5G
Copy the code


2.1 Setting the memory of the new generation

There are two ways to set the memory size of the new generation:

through-XX:NewSize-XX:MaxNewSize

  • -XX:NewSize: Configures the minimum memory of the new generation
  • -XX:MaxNewSize: Configures the maximum memory of the new generation

When we set the minimum and maximum memory to the same, we can also use the -xmn parameter

  • -XmnSet new generation size

Example:

- XX: NesSize1G - XX: MaxNesSize1G or - Xmn1GCopy the code

Little knowledge: An important lesson of GC tuning is to try to reserve new objects for the new generation. Since the cost of Full GC is much higher than that of Minor GC, it is advisable to allocate objects to the new generation as much as possible. In actual projects, the GC logs are used to analyze whether the allocation of new generation space is reasonable. Appropriately adjust the size of the new generation by using the “-xmn” command to minimize the situation that the new object directly enters the old age.


2.2 Setting the size of the permanent generation/meta-space

After JDK1.8, the permanent generation was changed to a meta-space, and if we do not specify the size of the meta-space, it may run out of available memory as the class is created

1) Set the permanent generation size

  • -XX:PermSize: Sets the initial size of the permanent generation
  • -XX:MaxPermSize: Sets the maximum size of the permanent generation

2) Set the size of the meta-space

  • -XX:MetaspaceSize: Sets the initial size of the meta-space
  • -XX:MaxMetaspaceSize: Sets the maximum size of the metacase

Note that:

  • The default for a 64-bit server-side JVM-XX:MetaspaceSizeOnce hit, Full GC will trigger and unload useless classes (that is, their corresponding classloaders are no longer alive). Then the high watermark will reset, depending on how much space is freed after GC. If the free space is insufficient, then in no more thanMaxMetaspaceSize, appropriately increase the value. If too much space is freed, lower this value appropriately
  • If the initial high water level is set too low, this high water level adjustment will occur many times, and multiple calls to the Full GC will be observed in the garbage collection logs. To avoid frequent GC, you can change the-XX:MetaspaceSizeSet to a relatively high value.


2.3 Other parameters of heap space

  • -xx :NewRatio: Set the ratio of Cenozoic era to old age. -xx :NewRatio=4 indicates that the ratio of Cenozoic era to old age is 1:4, and the Cenozoic era accounts for one-fifth of the whole heap. If -xmn is set, this parameter does not need to be set.

  • -xx :MaxTenuringThreshold: indicates the number of objects to survive in the new generation. The default value is 15. If the object is still alive after a MinorGC in Eden, it is moved to Survior, and age is increased by 1. After that, the age of the object increases by 1 each time the object experiences MinorGC. Reach the threshold, then move to the old age.

  • -xx :SurvivorRatio: ratio of Eden zone to Subrvivor zone. If set to 8, the ratio of two Subrvivor zones to one Eden zone is 2:8, and one Survivor zone accounts for 1/10 of the whole new generation.


3. Summary

In a typical corporate project, where there is an automated pipeline and limited server resources, a default startup parameter is set, we should consider whether we will run out of memory and whether GC will occur frequently during development

After all, JVM tuning is a last resort