总结一下GC中可能用到的命令做个备用把
(1)
打印GC,使用G1
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+UseG1GC
(2)
生成日志文件,在target目录下
-XX:+PrintGCDetails -verbose:gc
-XX:+PrintGCTimeStamps -Xloggc:gc.log
(3)
开启压缩指针参数
-XX:+UseCompressedClassPointers
-XX:+UseCompressedOops
-XX:-UseLargePagesIndividualAllocation
(4)
GC造成应用程序暂停的时间
-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCApplicationConcurrentTime
示例显示:
0.100: Total time for which application threads were stopped: 0.0291658 seconds, Stopping threads took: 0.0000201 seconds
0.129: Application time: 0.0001754 seconds
0.129: Total time for which application threads were stopped: 0.0001021 seconds, Stopping threads took: 0.0000802 seconds
1.129: Application time: 0.9638694 seconds
1.129: Total time for which application threads were stopped: 0.0000554 seconds, Stopping threads took: 0.0000197 seconds
4.130: Application time: 3.0008522 seconds
4.130: Total time for which application threads were stopped: 0.0000737 seconds, Stopping threads took: 0.0000357 seconds
4.204: Application time: 0.0737265 seconds
4.204: Total time for which application threads were stopped: 0.0000726 seconds, Stopping threads took: 0.0000215 seconds
5.228: Application time: 1.0243025 seconds
5.228: Total time for which application threads were stopped: 0.0000547 seconds, Stopping threads took: 0.0000193 seconds
(5)
与应用程序并行执行GC的线程数量,过大会过分占据应用程序cpu,过小会降低GC效率
-XX:ConcGCThreads=4
(6)
使用G1收集器并指定region大小,region大可以方便容纳大对象,但是一位增加region也不可取,region太小会降低G1灵活性
-XX:+UseG1GC
-XX:G1HeapRegionSize=32M
示例显示:
未指定region大小
garbage-first heap total 129024K, used 3072K [0x0000000082000000, 0x00000000821003f0, 0x0000000100000000)
region size 1024K, 4 young (4096K), 0 survivors (0K)
Metaspace used 4238K, capacity 4716K, committed 4864K, reserved 1056768K
class space used 463K, capacity 468K, committed 512K, reserved 1048576K指定了region大小
garbage-first heap total 131072K, used 0K [0x0000000082000000, 0x0000000084000020, 0x0000000100000000)
region size 32768K, 1 young (32768K), 0 survivors (0K)
Metaspace used 4237K, capacity 4716K, committed 4864K, reserved 1056768K
class space used 463K, capacity 468K, committed 512K, reserved 1048576K
(7)
G1的并发标记阶段调优参数
InitiatingHeapOccupancyPercent指的是堆占用高过N%后,就会触发并发GC周期,项目没有大的CPU负载时可以适当降低提前CMS从而减少Full GC
HeapWastePercent指的是全局并发标记结束后统计所有能被回收的垃圾占heap的比例高于5%则触发MixedGC
-XX:InitiatingHeapOccupancyPercent = 45
-XX:G1HeapWastePercent = 10
示例显示
采用了参数之后调小了两个参数之后,停顿时间变短了,应用程序时间变长了,因为提前回收,所以垃圾少的时候就开始标记,停顿时间变小,吞吐变高
0.100: Total time for which application threads were stopped: 0.0000759 seconds, Stopping threads took: 0.0000438 seconds
0.100: Application time: 0.0000372 seconds
0.100: Total time for which application threads were stopped: 0.0000182 seconds, Stopping threads took: 0.0000088 seconds
1.100: Application time: 0.9539404 seconds
1.100: Total time for which application threads were stopped: 0.0000594 seconds, Stopping threads took: 0.0000186 seconds
4.101: Application time: 3.0013960 seconds
4.101: Total time for which application threads were stopped: 0.0000485 seconds, Stopping threads took: 0.0000233 seconds
4.185: Application time: 0.0837444 seconds
4.185: Total time for which application threads were stopped: 0.0000642 seconds, Stopping threads took: 0.0000197 seconds
5.220: Application time: 1.0349334 seconds
5.220: Total time for which application threads were stopped: 0.0000397 seconds, Stopping threads took: 0.0000182 seconds
Heap
garbage-first heap total 129024K, used 3072K [0x0000000082000000, 0x00000000821003f0, 0x0000000100000000)
region size 1024K, 4 young (4096K), 0 survivors (0K)
Metaspace used 4236K, capacity 4716K, committed 4864K, reserved 1056768K
class space used 463K, capacity 468K, committed 512K, reserved 1048576K
没采用参数之前
0.095: Total time for which application threads were stopped: 0.0001182 seconds, Stopping threads took: 0.0000784 seconds
0.095: Application time: 0.0001058 seconds
0.096: Total time for which application threads were stopped: 0.0001214 seconds, Stopping threads took: 0.0001014 seconds
1.096: Application time: 0.9425743 seconds
1.096: Total time for which application threads were stopped: 0.0000656 seconds, Stopping threads took: 0.0000208 seconds
4.096: Application time: 3.0006188 seconds
4.097: Total time for which application threads were stopped: 0.0000419 seconds, S