[Java] JVM GC configuration guide

1. JDK version

All the following optimizations are based on JDK8the version. It is strongly recommended to upgrade to the lower version JDK8and use update_191the later version as much as possible.

2. How to choose a garbage collector

Response-first application: For C-side applications that are sensitive to response time, it is recommended to choose G1 if the heap memory is above 8G, and choose CMS if the heap memory is small or the version of JDK is low;

Throughput priority application: It is recommended to select applications that are not sensitive to response time and target high throughput (such as MQ and Worker) ParallelGC;

3. Optimize parameters of each collector

1) Basic parameter configuration (required for all applications and all collectors):

-Xmx(一般为容器内存的50%)

-Xms(与Xmx一致)

-XX:MetaspaceSize(通常256M~512M)

-XX:ParallelGCThreads=容器核数

-XX:CICompilerCount=容器核数(必须大于等于2)

2)ParallelGC

In addition to the above parameters, generally no additional tuning is required (JDK8 default collector)

3)CMS

-XX:+UseConcMarkSweepGC

-Xmn (一般为堆内存的三分之一),尤其是配置了ParallelGCThreads后必须配置此参数

-XX:ConcGCThreads=n(默认为ParallelGCThreads/4,可视情况调整至ParallelGCThreads/2)

-XX:+UseCMSInitiatingOccupancyOnly

-XX:CMSInitiatingOccupancyFraction=70(推荐值)

4)G1

-XX:+UseG1GC

-XX:ConcGCThreads=n(默认为ParallelGCThreads/4,可视情况调整至ParallelGCThreads/2)

-XX:G1HeapRegionSize=8m(若堆内存在8G以内且有较多大对象推荐设置此值)

*Be careful not to set -Xmn and XX:NewRatio

5) Other tuning parameters

-XX:+ParallelRefProcEnabled If Referencethe processing time of GC is long, for example, a large number WeakReferenceof objects are used, parallel processing can be enabled through this parameter

4. Turn on the GC log

-XX:+PrintGCDetails

-XX:+PrintGCDateStamps

-Xloggc:/export/Logs/gc.log

5. How to judge whether the GC is normal

1) GC Whether it is frequent: YoungGC the frequency is generally once in tens of seconds, FullGCusually several times a day, and G1the collector should not appear FullGC;

2) GCTime-consuming: The time-consuming mainly depends on the size of the heap memory and the number of garbage objects. YoungGCThe time should usually be tens of milliseconds, FullGCusually hundreds of milliseconds;

3) Whether the GC memory drops each time: When the application is just started, YoungGCthe memory should be recycled to a lower water level each time. As time goes by, the old generation will gradually increase, and the memory water level will gradually rise until the FullGC/MixedGC(G1)memory will return to the lower water level again. Otherwise there may be a memory leak;

4) If used ParallelGC, it will only be triggered when the heap memory is exhausted FullGC, so there is no need to configure the heap memory usage alarm, but you need to pay attention to GCthe frequency;

Guess you like

Origin blog.csdn.net/ihero/article/details/132212049
Recommended