JVM调优实战

1、环境准备

需要调优的项目是个springboot 搭建的一个eureka服务,百度云下载链接地址附在线面。
链接:https://pan.baidu.com/s/1q-vnmSwg5e05lqVKnQdJPw
提取码:r7o4

2、JVM调优主要就是调整下面两个指标

  1. 停顿时间: 垃圾收集器做垃圾回收中断应用执行的时间。 -XX:MaxGCPauseMillis
  2. 吞吐量:垃圾收集的时间和总时间的占比:1/(1+n),吞吐量为1-1/(1+n) 。 XX:GCTimeRatio=n

3、GC调优步骤

打印GC日志

java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:./gc.log
Tomcat则直接加在JAVA_OPTS变量里
分析日志得到关键性指标
分析GC原因,调优JVM参数

E:\jvmtest>java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:./parallelgc.log -jar eureka-server.jar

http://localhost:8761 项目启动成功
会在当前的项目目录下生产长城一个parallelgc.log 文件
1、Parallel Scavenge收集器(默认)
分析parallel-gc.log

Java HotSpot(TM) 64-Bit Server VM (25.102-b14) for windows-amd64 JRE (1.8.0_102-b14), built on Jun 22 2016 13:15:21 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 8277748k(4290892k free), swap 19812084k(14246140k free)
CommandLine flags: -XX:InitialHeapSize=132443968 -XX:MaxHeapSize=2119103488 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
2020-02-23T17:55:29.373+0800: 2.742: [GC (Allocation Failure) [PSYoungGen: 33280K->3036K(38400K)] 33280K->3044K(125952K), 0.0219619 secs] [Times: user=0.00 sys=0.00, real=0.02 secs] 
2020-02-23T17:55:29.987+0800: 3.355: [GC (Allocation Failure) [PSYoungGen: 36316K->3551K(38400K)] 36324K->3567K(125952K), 0.0042876 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:30.582+0800: 3.950: [GC (Allocation Failure) [PSYoungGen: 36831K->2896K(38400K)] 36847K->2920K(125952K), 0.0025079 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T17:55:31.149+0800: 4.516: [GC (Allocation Failure) [PSYoungGen: 36176K->4153K(38400K)] 36200K->4185K(125952K), 0.0036800 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T17:55:31.351+0800: 4.719: [GC (Allocation Failure) [PSYoungGen: 37433K->4148K(38400K)] 37465K->4188K(125952K), 0.0055490 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:31.509+0800: 4.877: [GC (Allocation Failure) [PSYoungGen: 37428K->4321K(71680K)] 37468K->4369K(159232K), 0.0048466 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T17:55:31.790+0800: 5.158: [GC (Allocation Failure) [PSYoungGen: 70881K->3137K(71680K)] 70929K->5532K(159232K), 0.0067275 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:32.125+0800: 5.493: [GC (Allocation Failure) [PSYoungGen: 69697K->2457K(138240K)] 72092K->5316K(225792K), 0.0043441 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:33.290+0800: 6.658: [GC (Allocation Failure) [PSYoungGen: 135577K->4555K(138240K)] 138436K->7919K(225792K), 0.0102491 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:33.471+0800: 6.840: [GC (Metadata GC Threshold) [PSYoungGen: 8559K->2080K(218624K)] 11923K->6374K(306176K), 0.0069958 secs] [Times: user=0.05 sys=0.02, real=0.01 secs] 
2020-02-23T17:55:33.478+0800: 6.847: [Full GC (Metadata GC Threshold) [PSYoungGen: 2080K->0K(218624K)] [ParOldGen: 4294K->6162K(53760K)] 6374K->6162K(272384K), [Metaspace: 20910K->20910K(1069056K)], 0.0849991 secs] [Times: user=0.27 sys=0.00, real=0.09 secs] 
2020-02-23T17:55:35.322+0800: 8.689: [GC (Allocation Failure) [PSYoungGen: 212992K->4993K(218624K)] 219154K->11163K(272384K), 0.0069890 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:36.156+0800: 9.524: [GC (Allocation Failure) [PSYoungGen: 217985K->4963K(287744K)] 224155K->13556K(341504K), 0.0100496 secs] [Times: user=0.05 sys=0.02, real=0.01 secs] 
2020-02-23T17:55:37.966+0800: 11.334: [GC (Allocation Failure) [PSYoungGen: 287587K->6115K(290304K)] 296180K->20073K(344064K), 0.0138193 secs] [Times: user=0.03 sys=0.03, real=0.01 secs] 
2020-02-23T17:55:39.469+0800: 12.837: [GC (Allocation Failure) [PSYoungGen: 290275K->5986K(377856K)] 304233K->25904K(431616K), 0.0219192 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
2020-02-23T17:55:40.511+0800: 13.879: [GC (Metadata GC Threshold) [PSYoungGen: 193610K->3014K(378368K)] 213527K->26488K(432128K), 0.0121948 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:40.524+0800: 13.891: [Full GC (Metadata GC Threshold) [PSYoungGen: 3014K->0K(378368K)] [ParOldGen: 23474K->22776K(99840K)] 26488K->22776K(478208K), [Metaspace: 35039K->35039K(1081344K)], 0.1210607 secs] [Times: user=0.33 sys=0.00, real=0.12 secs] 
2020-02-23T17:55:42.068+0800: 15.435: [GC (Allocation Failure) [PSYoungGen: 369152K->5091K(471552K)] 391928K->27876K(571392K), 0.0121537 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 
2020-02-23T17:55:44.289+0800: 17.657: [GC (Allocation Failure) [PSYoungGen: 471523K->8680K(486912K)] 494308K->32747K(586752K), 0.0181657 secs] [Times: user=0.05 sys=0.01, real=0.02 secs] 
2020-02-23T17:55:46.507+0800: 19.875: [GC (Allocation Failure) [PSYoungGen: 486888K->8755K(553984K)] 510955K->41063K(653824K), 0.0341341 secs] [Times: user=0.17 sys=0.00, real=0.03 secs] 

上面信息显示了年轻代和老年代每次gc 之后的内存变化。
调优
第一次调优,设置Metaspace大小:增大元空间大小-XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=64M
第二次调优,增大年轻代动态扩容增量,默认是20(%),可以减少young gc:-XX:YoungGenerationSizeIncrement=30
比较下几次调优效果:
吞吐量 最大停顿 平均停顿 Young gc Full gc
98.356% 120 ms 19 ms 19 2
99.252% 20 ms 10 ms 16 0

99.24% 17
2、配置CMS收集器
java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+UseConcMarkSweepGC -Xloggc:./cmsgc.log -jar eureka-server.jar

Java HotSpot(TM) 64-Bit Server VM (25.102-b14) for windows-amd64 JRE (1.8.0_102-b14), built on Jun 22 2016 13:15:21 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 8277748k(4378560k free), swap 19812084k(14299904k free)
CommandLine flags: -XX:InitialHeapSize=132443968 -XX:MaxHeapSize=2119103488 -XX:MaxNewSize=348966912 -XX:MaxTenuringThreshold=6 -XX:OldPLABSize=16 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC 
2020-02-23T18:05:11.706+0800: 0.621: [GC (Allocation Failure) 2020-02-23T18:05:11.706+0800: 0.621: [ParNew: 34944K->3012K(39296K), 0.0051010 secs] 34944K->3012K(126720K), 0.0056178 secs] [Times: user=0.05 sys=0.02, real=0.01 secs] 
2020-02-23T18:05:12.027+0800: 0.942: [GC (Allocation Failure) 2020-02-23T18:05:12.027+0800: 0.942: [ParNew: 37956K->2541K(39296K), 0.0085515 secs] 37956K->4544K(126720K), 0.0086613 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:12.567+0800: 1.482: [GC (Allocation Failure) 2020-02-23T18:05:12.567+0800: 1.482: [ParNew: 37485K->1180K(39296K), 0.0028535 secs] 39488K->3183K(126720K), 0.0029385 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:12.724+0800: 1.638: [GC (Allocation Failure) 2020-02-23T18:05:12.724+0800: 1.638: [ParNew: 36124K->2724K(39296K), 0.0026536 secs] 38127K->4726K(126720K), 0.0027267 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:12.811+0800: 1.726: [GC (Allocation Failure) 2020-02-23T18:05:12.811+0800: 1.726: [ParNew: 37668K->2410K(39296K), 0.0020314 secs] 39670K->4412K(126720K), 0.0021021 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:12.892+0800: 1.807: [GC (Allocation Failure) 2020-02-23T18:05:12.892+0800: 1.807: [ParNew: 37354K->2887K(39296K), 0.0026690 secs] 39356K->4890K(126720K), 0.0027718 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:12.959+0800: 1.873: [GC (Allocation Failure) 2020-02-23T18:05:12.959+0800: 1.873: [ParNew: 37831K->2767K(39296K), 0.0030040 secs] 39834K->5154K(126720K), 0.0030890 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.070+0800: 1.985: [GC (Allocation Failure) 2020-02-23T18:05:13.070+0800: 1.985: [ParNew: 37711K->3022K(39296K), 0.0026935 secs] 40098K->5409K(126720K), 0.0027927 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.181+0800: 2.096: [GC (Allocation Failure) 2020-02-23T18:05:13.181+0800: 2.096: [ParNew: 37966K->2871K(39296K), 0.0030274 secs] 40353K->5455K(126720K), 0.0031111 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.284+0800: 2.199: [GC (Allocation Failure) 2020-02-23T18:05:13.284+0800: 2.199: [ParNew: 37815K->2818K(39296K), 0.0033782 secs] 40399K->5675K(126720K), 0.0034781 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.482+0800: 2.397: [GC (Allocation Failure) 2020-02-23T18:05:13.482+0800: 2.397: [ParNew: 37762K->3641K(39296K), 0.0049027 secs] 40619K->6653K(126720K), 0.0050114 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.646+0800: 2.561: [GC (Allocation Failure) 2020-02-23T18:05:13.646+0800: 2.561: [ParNew: 38585K->3119K(39296K), 0.0046017 secs] 41597K->6669K(126720K), 0.0046886 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.826+0800: 2.740: [GC (Allocation Failure) 2020-02-23T18:05:13.826+0800: 2.740: [ParNew: 38063K->3435K(39296K), 0.0054518 secs] 41613K->8093K(126720K), 0.0055253 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:13.831+0800: 2.746: [GC (CMS Initial Mark) [1 CMS-initial-mark: 4657K(87424K)] 8794K(126720K), 0.0008727 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.832+0800: 2.747: [CMS-concurrent-mark-start]
2020-02-23T18:05:13.859+0800: 2.774: [CMS-concurrent-mark: 0.027/0.027 secs] [Times: user=0.03 sys=0.03, real=0.03 secs] 
2020-02-23T18:05:13.859+0800: 2.774: [CMS-concurrent-preclean-start]
2020-02-23T18:05:13.861+0800: 2.775: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:13.861+0800: 2.775: [CMS-concurrent-abortable-preclean-start]
2020-02-23T18:05:13.983+0800: 2.898: [GC (Allocation Failure) 2020-02-23T18:05:13.983+0800: 2.898: [ParNew: 38379K->2894K(39296K), 0.0043386 secs] 43037K->8267K(126720K), 0.0057849 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:14.175+0800: 3.090: [CMS-concurrent-abortable-preclean: 0.135/0.315 secs] [Times: user=0.66 sys=0.14, real=0.31 secs] 
2020-02-23T18:05:14.175+0800: 3.090: [GC (CMS Final Remark) [YG occupancy: 21406 K (39296 K)]2020-02-23T18:05:14.175+0800: 3.090: [Rescan (parallel) , 0.0032545 secs]2020-02-23T18:05:14.179+0800: 3.093: [weak refs processing, 0.0000356 secs]2020-02-23T18:05:14.179+0800: 3.093: [class unloading, 0.0027417 secs]2020-02-23T18:05:14.181+0800: 3.096: [scrub symbol table, 0.0033667 secs]2020-02-23T18:05:14.185+0800: 3.099: [scrub string table, 0.0004065 secs][1 CMS-remark: 5373K(87424K)] 26779K(126720K), 0.0105055 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
2020-02-23T18:05:14.186+0800: 3.100: [CMS-concurrent-sweep-start]
2020-02-23T18:05:14.188+0800: 3.102: [CMS-concurrent-sweep: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:14.188+0800: 3.102: [CMS-concurrent-reset-start]
2020-02-23T18:05:14.189+0800: 3.104: [CMS-concurrent-reset: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:14.563+0800: 3.478: [GC (Allocation Failure) 2020-02-23T18:05:14.563+0800: 3.478: [ParNew: 37838K->2334K(39296K), 0.0025628 secs] 43117K->7613K(126720K), 0.0026493 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:14.684+0800: 3.599: [GC (Allocation Failure) 2020-02-23T18:05:14.684+0800: 3.599: [ParNew: 37278K->2569K(39296K), 0.0029898 secs] 42557K->7848K(126720K), 0.0030795 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:14.837+0800: 3.752: [GC (Allocation Failure) 2020-02-23T18:05:14.837+0800: 3.752: [ParNew: 37513K->3762K(39296K), 0.0037752 secs] 42792K->9041K(126720K), 0.0038503 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:14.867+0800: 3.782: [GC (Allocation Failure) 2020-02-23T18:05:14.867+0800: 3.782: [ParNew: 38706K->2480K(39296K), 0.0041355 secs] 43985K->8678K(126720K), 0.0042114 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:14.985+0800: 3.899: [GC (Allocation Failure) 2020-02-23T18:05:14.985+0800: 3.899: [ParNew: 37424K->4100K(39296K), 0.0033557 secs] 43622K->10297K(126720K), 0.0034520 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:15.100+0800: 4.014: [GC (Allocation Failure) 2020-02-23T18:05:15.100+0800: 4.015: [ParNew: 39044K->3614K(39296K), 0.0039214 secs] 45241K->10424K(126720K), 0.0040427 secs] [Times: user=0.05 sys=0.02, real=0.00 secs] 
2020-02-23T18:05:15.189+0800: 4.103: [GC (Allocation Failure) 2020-02-23T18:05:15.189+0800: 4.103: [ParNew: 38558K->2902K(39296K), 0.0030234 secs] 45368K->9983K(126720K), 0.0030949 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:15.273+0800: 4.188: [GC (Allocation Failure) 2020-02-23T18:05:15.274+0800: 4.188: [ParNew: 37846K->3559K(39296K), 0.0041845 secs] 44927K->11208K(126720K), 0.0042904 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:15.400+0800: 4.315: [GC (Allocation Failure) 2020-02-23T18:05:15.400+0800: 4.315: [ParNew: 38503K->2821K(39296K), 0.0044654 secs] 46152K->11446K(126720K), 0.0045570 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:15.561+0800: 4.475: [GC (Allocation Failure) 2020-02-23T18:05:15.561+0800: 4.475: [ParNew: 37765K->2744K(39296K), 0.0030013 secs] 46390K->11370K(126720K), 0.0030831 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:15.750+0800: 4.664: [GC (Allocation Failure) 2020-02-23T18:05:15.750+0800: 4.664: [ParNew: 37688K->3961K(39296K), 0.0034923 secs] 46314K->12973K(126720K), 0.0035642 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:15.917+0800: 4.831: [GC (Allocation Failure) 2020-02-23T18:05:15.917+0800: 4.831: [ParNew: 38905K->3119K(39296K), 0.0039506 secs] 47917K->12564K(126720K), 0.0041086 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:16.320+0800: 5.235: [GC (Allocation Failure) 2020-02-23T18:05:16.321+0800: 5.235: [ParNew: 38063K->3467K(39296K), 0.0063162 secs] 47508K->13637K(126720K), 0.0064336 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:20.073+0800: 8.987: [GC (Allocation Failure) 2020-02-23T18:05:20.073+0800: 8.987: [ParNew: 38411K->3483K(39296K), 0.0042572 secs] 48581K->14364K(126720K), 0.0043595 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:20.154+0800: 9.069: [GC (Allocation Failure) 2020-02-23T18:05:20.154+0800: 9.069: [ParNew: 38427K->2768K(39296K), 0.0033703 secs] 49308K->14070K(126720K), 0.0034710 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:20.248+0800: 9.163: [GC (Allocation Failure) 2020-02-23T18:05:20.248+0800: 9.163: [ParNew: 37712K->3654K(39296K), 0.0030455 secs] 49014K->14955K(126720K), 0.0031332 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:20.343+0800: 9.258: [GC (Allocation Failure) 2020-02-23T18:05:20.343+0800: 9.258: [ParNew: 38598K->2305K(39296K), 0.0044966 secs] 49899K->14687K(126720K), 0.0045847 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:20.685+0800: 9.600: [GC (Allocation Failure) 2020-02-23T18:05:20.685+0800: 9.600: [ParNew: 37249K->2923K(39296K), 0.0037934 secs] 49631K->15305K(126720K), 0.0038728 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:20.835+0800: 9.750: [GC (Allocation Failure) 2020-02-23T18:05:20.835+0800: 9.750: [ParNew: 37867K->4352K(39296K), 0.0052278 secs] 50249K->18435K(126720K), 0.0053187 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:21.025+0800: 9.940: [GC (Allocation Failure) 2020-02-23T18:05:21.025+0800: 9.940: [ParNew: 39296K->3890K(39296K), 0.0066821 secs] 53379K->22340K(126720K), 0.0067840 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2020-02-23T18:05:21.194+0800: 10.108: [GC (Allocation Failure) 2020-02-23T18:05:21.194+0800: 10.108: [ParNew: 38834K->1870K(39296K), 0.0024154 secs] 57284K->20320K(126720K), 0.0025158 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
2020-02-23T18:05:21.296+0800: 10.210: [GC (Allocation Failure) 2020-02-23T18:05:21.296+0800: 10.210: [ParNew: 36814K->1642K(39296K), 0.0025098 secs] 55264K->20092K(126720K), 0.0025794 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 

image.png

对应着cms 垃圾回收器的垃圾回收的一个过程。


image.png

3、配置G1收集器
-XX:+UseG1GC
java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+UseG1GC -Xloggc:./g1gc.log -jar eureka-server.jar
g1gc.log

Java HotSpot(TM) 64-Bit Server VM (25.102-b14) for windows-amd64 JRE (1.8.0_102-b14), built on Jun 22 2016 13:15:21 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 8277748k(3842868k free), swap 19812084k(13363324k free)
CommandLine flags: -XX:InitialHeapSize=132443968 -XX:MaxHeapSize=2119103488 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC -XX:-UseLargePagesIndividualAllocation 
2020-02-23T18:24:15.298+0800: 0.395: [GC pause (G1 Evacuation Pause) (young), 0.0030404 secs]
   [Parallel Time: 2.4 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 395.2, Avg: 395.2, Max: 395.3, Diff: 0.1]
      [Ext Root Scanning (ms): Min: 0.2, Avg: 0.6, Max: 1.0, Diff: 0.8, Sum: 2.2]
      [Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Processed Buffers: Min: 0, Avg: 0.0, Max: 0, Diff: 0, Sum: 0]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.3, Diff: 0.3, Sum: 0.6]
      [Object Copy (ms): Min: 1.2, Avg: 1.5, Max: 1.8, Diff: 0.5, Sum: 6.1]
      [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Termination Attempts: Min: 1, Avg: 1.5, Max: 2, Diff: 1, Sum: 6]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.1, Max: 0.1, Diff: 0.0, Sum: 0.3]
      [GC Worker Total (ms): Min: 2.2, Avg: 2.3, Max: 2.3, Diff: 0.1, Sum: 9.2]
      [GC Worker End (ms): Min: 397.5, Avg: 397.5, Max: 397.5, Diff: 0.0]
   [Code Root Fixup: 0.1 ms]
   [Code Root Purge: 0.0 ms]
   [Clear CT: 0.1 ms]
   [Other: 0.5 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 0.3 ms]
      [Ref Enq: 0.0 ms]
      [Redirty Cards: 0.1 ms]
      [Humongous Register: 0.0 ms]
      [Humongous Reclaim: 0.0 ms]
      [Free CSet: 0.0 ms]
   [Eden: 14.0M(14.0M)->0.0B(18.0M) Survivors: 0.0B->2048.0K Heap: 14.0M(128.0M)->1895.5K(128.0M)]
 [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:24:15.544+0800: 0.640: [GC pause (G1 Evacuation Pause) (young), 0.0032265 secs]
   [Parallel Time: 2.3 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 640.2, Avg: 640.2, Max: 640.2, Diff: 0.0]
      [Ext Root Scanning (ms): Min: 0.2, Avg: 0.3, Max: 0.6, Diff: 0.4, Sum: 1.3]
      [Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Processed Buffers: Min: 0, Avg: 0.0, Max: 0, Diff: 0, Sum: 0]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.4, Diff: 0.4, Sum: 0.5]
      [Object Copy (ms): Min: 1.6, Avg: 1.8, Max: 2.0, Diff: 0.4, Sum: 7.0]
      [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Termination Attempts: Min: 1, Avg: 4.0, Max: 9, Diff: 8, Sum: 16]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [GC Worker Total (ms): Min: 2.2, Avg: 2.2, Max: 2.2, Diff: 0.0, Sum: 8.9]
      [GC Worker End (ms): Min: 642.4, Avg: 642.4, Max: 642.4, Diff: 0.0]
   [Code Root Fixup: 0.1 ms]
   [Code Root Purge: 0.0 ms]
   [Clear CT: 0.1 ms]
   [Other: 0.8 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 0.7 ms]
      [Ref Enq: 0.0 ms]
      [Redirty Cards: 0.0 ms]
      [Humongous Register: 0.0 ms]
      [Humongous Reclaim: 0.0 ms]
      [Free CSet: 0.0 ms]
   [Eden: 18.0M(18.0M)->0.0B(73.0M) Survivors: 2048.0K->3072.0K Heap: 19.9M(128.0M)->4360.8K(128.0M)]
 [Times: user=0.00 sys=0.00, real=0.00 secs] 
2020-02-23T18:24:16.413+0800: 1.508: [GC pause (G1 Evacuation Pause) (young), 0.0053286 secs]
   [Parallel Time: 3.7 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 1508.5, Avg: 1508.6, Max: 1508.7, Diff: 0.2]
      [Ext Root Scanning (ms): Min: 0.1, Avg: 0.4, Max: 0.9, Diff: 0.8, Sum: 1.7]
      [Update RS (ms): Min: 0.0, Avg: 0.3, Max: 0.5, Diff: 0.5, Sum: 1.1]
         [Processed Buffers: Min: 0, Avg: 2.3, Max: 6, Diff: 6, Sum: 9]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.5, Max: 1.9, Diff: 1.9, Sum: 2.1]
      [Object Copy (ms): Min: 0.0, Avg: 1.4, Max: 2.0, Diff: 2.0, Sum: 5.6]
      [Termination (ms): Min: 0.0, Avg: 0.5, Max: 1.0, Diff: 1.0, Sum: 2.0]
         [Termination Attempts: Min: 1, Avg: 1.0, Max: 1, Diff: 0, Sum: 4]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [GC Worker Total (ms): Min: 2.8, Avg: 3.2, Max: 3.7, Diff: 0.9, Sum: 12.7]
      [GC Worker End (ms): Min: 1511.3, Avg: 1511.7, Max: 1512.2, Diff: 0.9]
   [Code Root Fixup: 0.1 ms]
   [Code Root Purge: 0.0 ms]
   [Clear CT: 0.1 ms]
   [Other: 1.5 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 1.3 ms]
      [Ref Enq: 0.0 ms]
      [Redirty Cards: 0.0 ms]
      [Humongous Register: 0.0 ms]
      [Humongous Reclaim: 0.0 ms]
      [Free CSet: 0.1 ms]
   [Eden: 73.0M(73.0M)->0.0B(73.0M) Survivors: 3072.0K->3072.0K Heap: 77.3M(128.0M)->4178.6K(128.0M)]
 [Times: user=0.06 sys=0.00, real=0.00 secs] 

young GC:[GC pause (G1 Evacuation Pause) (young)
initial-mark:[GC pause (Metadata GC Threshold) (young) (initial-mark) (参数:InitiatingHeapOccupancyPercent)
mixed GC:[GC pause (G1 Evacuation Pause) (mixed) (参数:G1HeapWastePercent)
full GC:[Full GC (Allocation Failure) (无可用region)
(G1内部,前面提到的混合GC是非常重要的释放内存机制,它避免了G1出现Region没有可用的情况,否则就会触发Full GC事件。
CMS、Parallel、Serial GC都需要通过Full GC去压缩老年代并在这个过程中扫描整个老年代。G1的Full GC算法和Serial GC收集器完全一致。当一个Full GC发生时,整个Java堆执行一个完整的压缩,这样确保了最大的空余内存可用。G1的Full GC是一个单线程,它可能引起一个长时间的停顿时间,G1的设计目标是减少Full GC,满足应用性能目标。)
调优
第一次调优,设置Metaspace大小:增大元空间大小-XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=64M
第二次调优,添加吞吐量和停顿时间参数:-XX:GCTimeRatio=80 -XX:MaxGCPauseMillis=100

查看发生MixedGC的阈值:jinfo -flag InitiatingHeapOccupancyPercent 进程id

调优分析工具:

分析工具:gceasy,GCViewer。
本实例采用gceasy。
把刚刚程序运行生成的gc文件通过gceasy上传上去。

image.png

image.png

image.png

image.png

image.png

image.png

GC常用参数
堆栈设置
-Xss:每个线程的栈大小
-Xms:初始堆大小,默认物理内存的1/64
-Xmx:最大堆大小,默认物理内存的1/4
-Xmn:新生代大小
-XX:NewSize:设置新生代初始大小
-XX:NewRatio:默认2表示新生代占年老代的1/2,占整个堆内存的1/3。
-XX:SurvivorRatio:默认8表示一个survivor区占用1/8的Eden内存,即1/10的新生代内存。
-XX:MetaspaceSize:设置元空间大小
-XX:MaxMetaspaceSize:设置元空间最大允许大小,默认不受限制,JVM Metaspace会进行动态扩展。
垃圾回收统计信息
-XX:+PrintGC
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-Xloggc:filename
收集器设置
-XX:+UseSerialGC:设置串行收集器
-XX:+UseParallelGC:设置并行收集器
-XX:+UseParallelOldGC:老年代使用并行回收收集器
-XX:+UseParNewGC:在新生代使用并行收集器
-XX:+UseParalledlOldGC:设置并行老年代收集器
-XX:+UseConcMarkSweepGC:设置CMS并发收集器
-XX:+UseG1GC:设置G1收集器
-XX:ParallelGCThreads:设置用于垃圾回收的线程数
并行收集器设置
-XX:ParallelGCThreads:设置并行收集器收集时使用的CPU数。并行收集线程数。
-XX:MaxGCPauseMillis:设置并行收集最大暂停时间
-XX:GCTimeRatio:设置垃圾回收时间占程序运行时间的百分比。公式为1/(1+n)
-XX:YoungGenerationSizeIncrement:年轻代gc后扩容比例,默认是20(%)
CMS收集器设置
-XX:+UseConcMarkSweepGC:设置CMS并发收集器
-XX:+CMSIncrementalMode:设置为增量模式。适用于单CPU情况。
-XX:ParallelGCThreads:设置并发收集器新生代收集方式为并行收集时,使用的CPU数。并行收集线程数。
-XX:CMSFullGCsBeforeCompaction:设定进行多少次CMS垃圾回收后,进行一次内存压缩
-XX:+CMSClassUnloadingEnabled:允许对类元数据进行回收
-XX:UseCMSInitiatingOccupancyOnly:表示只在到达阀值的时候,才进行CMS回收
-XX:+CMSIncrementalMode:设置为增量模式。适用于单CPU情况
-XX:ParallelCMSThreads:设定CMS的线程数量
-XX:CMSInitiatingOccupancyFraction:设置CMS收集器在老年代空间被使用多少后触发
-XX:+UseCMSCompactAtFullCollection:设置CMS收集器在完成垃圾收集后是否要进行一次内存碎片的整理
G1收集器设置
-XX:+UseG1GC:使用G1收集器
-XX:ParallelGCThreads:指定GC工作的线程数量
-XX:G1HeapRegionSize:指定分区大小(1MB~32MB,且必须是2的幂),默认将整堆划分为2048个分区
-XX:GCTimeRatio:吞吐量大小,0-100的整数(默认9),值为n则系统将花费不超过1/(1+n)的时间用于垃圾收集
-XX:MaxGCPauseMillis:目标暂停时间(默认200ms)
-XX:G1NewSizePercent:新生代内存初始空间(默认整堆5%)
-XX:G1MaxNewSizePercent:新生代内存最大空间
-XX:TargetSurvivorRatio:Survivor填充容量(默认50%)
-XX:MaxTenuringThreshold:最大任期阈值(默认15)
-XX:InitiatingHeapOccupancyPercen:老年代占用空间超过整堆比IHOP阈值(默认45%),超过则执行混合收集
-XX:G1HeapWastePercent:堆废物百分比(默认5%)
-XX:G1MixedGCCountTarget:参数混合周期的最大总次数(默认8)

你可能感兴趣的:(JVM调优实战)