How to tune the JVM

 

In the last issue, we introduced the JVM memory model and the garbage collection algorithm  juejin.cn/post/691347 ....

Here we organize the performance tuning parameters of JVM GC, and adjust the parameters to ensure the reliability of our system.

  • -Xms3550m: initialize the heap size, the memory size used at the beginning, if it exceeds this size, it will automatically expand;

  • -Xmx3550m: The maximum heap size, which will be allocated by the initialization system. Although the initial heap size is used at the beginning, it can be dynamically expanded according to the situation, but the maximum heap size cannot be exceeded, otherwise outOfMemoryError will be thrown;

  • -Xmn: set the size of the young generation;

  • -XX:SurvivorRatio=4: The ratio of the Eden area to a Survivor area in the young generation. For example, setting it to 4 means that the ratio of two Survivors to Eden is 2:4, and one Survivor occupies 1/6 of the entire young generation;

  • -XX:NewRatio=3: New generation: 1:3 in the old generation;

  • -XX:MetaspaceSize=256m Metaspace initial value (this is a parameter after JDK8, because JDK8 removes permanent substitution and replaces it with metaspace. If it is JDK7 or previous versions, you can use -XX:PermSize=256m and XX :MaxPermSize=512m);

  • -XX:MaxMetaspaceSize=512m The maximum value of metaspace (note that this is off-heap memory, which is not included in the heap size memory);

  • -Xss=1m: Set the stack size of each thread, reducing this value means that the same memory space can generate more threads.

Choice of collector

  • -XX:+UseSerialGC: Use the combination of Serial (new generation) +Serial Old (old generation) (the default value of the virtual machine running in Client mode);

  • -XX:+UserParNewGC: Use the collector combination of ParNew (new generation) + Serial Old (old generation);

  • -XX:+UseParallelGC: Use the collector combination of Parallel Scavenge (new generation) + Serial Old (old generation);

  • -XX:+UseParallelOldGC: Use the collector combination of Parallel Scavenge (new generation) + Parallel Old (old generation);

  • -XX:+UseConcMarkSweepGC: Use the collector combination of ParNew + CMS + Serial Old for memory reclamation. The Serial Old collector is used as a backup collector after the Concurrent Mode Failure of the CMS collector;

  • -XX:+UseG1GC: Use G1 garbage collector (default value of Server mode after JDK9);

  • -XX:+UseZGC: Use ZGC collector;

  • -XX: UseShenandoahGC: Use Shenandoah collector.

Parameter settings of the collector

  • -XX:PretenureSizeThreshold: Promote directly to the size of the object in the old generation. When the size of the object exceeds this value, it will skip the new generation and enter the old generation;

  • -XX:MaxTenuringThreshold=0: Set the maximum age of garbage. If it is set to 0, the young generation object enters the old age directly without passing through the Survivor area, which can improve the efficiency of applications in the old age. If this value is set to a larger value, the young generation object will be copied multiple times in the Survivor area, which can increase the survival time of the object in the young generation and increase the probability that it will be recycled in the young generation;

  • -XX:UseAdapticeSizePolicy: dynamically adjust the size of each area in the java heap and the age when it enters the old age;

  • -XX:HandlePromotionFailure: Whether to allow allocation guarantee failure, that is, the remaining space in the old generation is not enough to cope with the extreme situation that all objects in the entire Eden and Survivir areas of the new generation survive;

  • -XX:ParallelGCThreads=n: Set the number of CPUs used by the parallel collector for collection. The number of parallel collection threads;

  • -XX:MaxGCPauseMillis=n: set the maximum pause time for parallel collection (only effective when using Parallel Scavenge and G1 collector);

  • -XX:GCTimeRatio=n: Set the percentage of garbage collection time to the running time of the program (only effective when using the Parallel Scavenge collector);

  • -XX:CMSInitiationOccupancyFraction: Set the CMS collector to trigger garbage collection after how much space is used in the old generation, the default value is 68% (only effective when using the CMS collector);

  • -XX:UseCMSCompactAtFullCollection: Set whether the CMS collector will perform a memory defragmentation after the garbage collection is completed (only effective when using the CMS collector, and will be abandoned in JDK9);

  • -XX:CMSFullGCsBeforeCompaction: Set the CMS collector to perform a number of garbage collectors and then start a memory defragmentation (only effective when the CMS collector is used, and will be abandoned in JDK9);

  • -XX:ConcGCThreads=n: Set the number of threads used by the concurrent garbage collector;

  • -XX: NewSizePecent: the minimum value of the new generation, the default value is 5% (G1);

  • -XX:MaxNewSizePecent: the maximum value of the new generation, the default value is 60% (G1);

  • -XX:G1ReservePercent=n: Set the total amount of heap memory reserved as false ceiling to reduce the possibility of promotion failure. The default value is 10 (G1);

  • -XX:G1HeapRegionSize=n: When using G1, the java heap will be divided into regions (regions) of uniform size. This parameter is to set the size of each heap region. The minimum is 1Mb and the maximum is 32Mb (G1);

  • InitiatingHeapOccupancyPercent: Set the threshold of the java heap occupancy rate of the trigger mark period. The default value is 45%. The java ratio here refers to non_young_capacity_bytes, including old+humongous;

  • ShenandoahGCHeuristics: When did Shenandoah start a GC process (specific to Shenandoah collector);

  • UseNUMA: Enable NUMA memory allocation support, currently only Parallel and ZGC support.

Minor GC and Major  GC

  • In most cases, objects are allocated in the Eden area of ​​the new generation. When the Eden area does not have enough space for allocation, the virtual machine will initiate a Minor GC. The new generation of Minor GC is very frequent, and the recovery speed is generally faster. Before the occurrence of Minor GC, the virtual machine first checks whether the maximum available continuous space in the old generation is greater than the total size of large objects in the new generation or the average object size of previous promotion to the old generation. If it is not satisfied, Full GC will be performed;

  • Major GC refers to the GC that occurred in the old age. Major GC is often accompanied by at least one Minor GC. The speed of Major GC is generally more than 10 times slower than that of Minor GC.

Conditions for the subject to enter the old age

  • Judge whether the object is a large object through the XX:PretenureSizeThreshold parameter introduced above, if it is, the object will directly enter the old age;

  • Objects older than the MaxTenuringThreshold introduced above will be promoted to the old age;

  • If the total size of all objects of the same age in the Survivor space is greater than half of the Survivor space, objects with an age greater than or equal to this age will also enter the old age directly

Causes of Full GC

  • The old age is overwritten;

  • Insufficient space for the permanent Pemannet Generation;

  • According to statistics, the size of Young GC promoted to the old generation is larger than the remaining space of the old generation;

  • System.gc() is called explicitly.

Tuning rules

  • According to different business conditions, the tuning parameters will be different. This can be monitored by performance monitoring tools and then accurate judgments can be made. For example, if you need to create a large number of short-lived objects, you can appropriately adjust the space size of the young generation to reduce the number of Minor GCs, because for virtual machines. The cost of copying objects is much higher than the cost of scanning. Therefore, the time spent in a single Minor GC depends more on the number of objects that survive the GC, rather than the size of the Eden area, so increasing the space can improve efficiency;

  • If you find that there are more Major GCs, you can increase the size of the old generation. At the same time, observe whether more space will be cleaned up after Major GC. If so, it means that there are many objects in the old generation that are not long-lived. You can consider Increase the value of -XX:MaxTenuringThreshold to prevent the object from entering the old age too early.

Guess you like

Origin blog.csdn.net/Baron_ND/article/details/112413434