First, tuning strategy
There are two indicators for GC performance: throughput (working time does not count the total time of GC) and pause (app cannot respond to the display when GC occurs).Copy the code
1. Purpose of tuning
The ultimate goal of tuning, of course, is to increase throughput and reduce pause times. Mapping to the GC level is mainly concerned with the following two things:
(1) Reduce the number of objects transferred to the old age to a minimum. (2) Reduce the execution time of full GC. (Minimize the number of GC)Copy the code
In which case the object will be transferred to the old age, there are four main types:
(1) The age of Cenozoic objects will be increased by one each time they undergo minor GC, and when they reach the age threshold, they will enter the old age directly. The threshold size is generally 15. (2) If the total size of all age objects in Survivor space is greater than half of the size of Survivor space, objects older than or equal to this age can directly enter the old age without waiting for the age threshold. (3) Big object directly into the old age. (4) The new generation replication algorithm needs a Survivor region for rotation backup. If a large number of objects survive after minor GC, the old age is required for allocation guarantee, so that the objects that are unable to accommodate survivor directly enter the old age.Copy the code
To reduce the number of full GC times, we need to first look at the two major categories of GC
Partial GC: Mode that does not collect the entire GC heap
Young GC: Collects only GC of Young Gen Old GC: collects only GC of Old Gen. Only CMS concurrent collection has this mode Mixed GC: collect the entire YOUNG Gen and part of the old Gen GC. Only the G1 has this modeCopy the code
Full GC: Global scope GC for the entire New generation, old generation, metaspace (java8 + replaces Perm Gen). Here’s why you want to reduce the number of Full GC’s.
A typical Full GC takes ten times as long as a Young GC.
2. What aspects can be considered for tuning?
To achieve the above goals, in general, you can consider tuning the following:
(1) Reduce the use of global variables and large objects.
(2) Whether the size of Cenozoic and old age is appropriate.
(3) Whether the proportion of new generation and old age is appropriate.
(4) Whether the proportion of survivor area and newborn area is appropriate.
(5) Select the appropriate GC collector.
3. What makes GC good?
In addition, GC tuning is not necessary if all of the following conditions are met for GC execution time:
Minor GC executes very quickly (less than 50ms) Minor GC does not execute very frequently (about every 10 seconds) Full GC executes very quickly (less than 1s) Full GC does not execute very frequently (about every 10 minutes)Copy the code
The numbers in parentheses are not absolute; they also vary with the state of the service.
Ii. Tuning Experience (Rules)
These rules, generally recommended, can be used as initial configuration recommendations, of course, the specific JVM tool monitoring for detailed analysis. (1) -xmx and -xms are generally set to the same size. This makes the GC run slightly more efficiently because he/she no longer has to estimate whether the heap needs to be resized.
(2) The new generation is officially recommended to make up 3/8 of the heap. (3) The surviving generation accounts for 1/10 of the Cenozoic generation. (4) Garbage collector if the memory is relatively large, G1 collector is recommended, of course, CMS collector can also be used. (5) -xx :+DisableExplicitGC disables system.gc () to prevent programmers from accidentally calling GC methods and affecting performance; (6) Throughput first applications: Generally, throughput first applications have a large young generation and a small old generation. The reason is that in this way, most short-term objects can be recycled as much as possible, while medium-term objects can be reduced, while the old generation stores long-term objects. (7) When adopting concurrent collection, the young generation is smaller, while the old generation is larger, because the old generation is mainly used for concurrent collection, even if it takes a long time, other programs will not be affected to continue to run. The website will not stop. (8) The advantage of using CMS is to use as few new generation as possible, and then use CMS for parallel collection of old generation, which can ensure the throughput efficiency of the system with low delay.Copy the code
Here are some parameters for JVM tuning:
‘-xmx300m maximum heap size -xms300m initial heap size -xmn100m young generation size -xx :SurvivorRatio=8 Eden and Survivor size ratio, If 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 whole young generation
-xx :+UseG1GC using G1 (Garbage First) Garbage collector -xx :MaxTenuringThreshold=14. -xx :ParallelGCThreads=8 Sets the number of threads to be used by the garbage collector during the parallel phase. [-xx :ConcGCThreads=8 Number of threads used by the concurrent garbage collector
-xx :+DisableExplicitGC Disallows explicit calls to System.gc() during startup
– XX: + HeapDumpOnOutOfMemoryError OOM export to file – XX: HeapDumpPath: = d/a. d. ump party export OOM path – XX: + PrintGCDetails print GC details -xx :+PrintGCTimeStamps Print CG occurrences in time stamps -xx :+PrintHeapAtGC before and after GC, Print heap information -xx :+TraceClassLoading Monitors class loading -xx :+PrintClassHistogram prints class information ‘after pressing Ctrl+Break