一、GC日志输出参数
前面通过-XX:+PrintGCDetails可以对GC日志进行打印,我们就可以在控制台查看,这样虽然可以查看GC的信息,但是并不直观,可以借助于第三方的GC日志分析工具进行查看。
在日志打印输出涉及到的参数如下:
‐XX:+PrintGC 输出GC日志
‐XX:+PrintGCDetails 输出GC的详细日志
‐XX:+PrintGCTimeStamps 输出GC的时间戳(以基准时间的形式)
‐XX:+PrintGCDateStamps 输出GC的时间戳(以日期的形式,如 2013‐05‐04T21:53:59.234+0800)
‐XX:+PrintHeapAtGC 在进行GC的前后打印出堆的信息
‐Xloggc:../logs/gc.log 日志文件的输出路径
1、编写测试代码
package com.zn; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Random; public class GCTest { public static void main(String[] args) throws Exception { List<Object> list = new ArrayList<Object>(); while (true) { int sleep = new Random().nextInt(100); if (System.currentTimeMillis() % 2 == 0) { list.clear(); } else { for (int i = 0; i < 10000; i++) { Properties properties = new Properties(); properties.put("key_" + i, "value_" + System.currentTimeMillis() + i); list.add(properties); } } // System.out.println("list大小为:" + list.size()); Thread.sleep(sleep); } } }
2、设置垃圾回收
-XX:+UseG1GC -XX:MaxGCPauseMillis=100 -Xmx256m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:E://accp//Y2//进阶内容//JVM//GCLogs//gc.log
3、启动程序
在E://accp//Y2//进阶内容//JVM//GCLogs下生成gc.log文件
Java HotSpot(TM) 64-Bit Server VM (25.144-b01) for windows-amd64 JRE (1.8.0_144-b01), built on Jul 21 2017 21:57:33 by "java_re" with MS VC++ 10.0 (VS2010) Memory: 4k page, physical 8293712k(2908244k free), swap 12356048k(6018612k free) CommandLine flags: -XX:InitialHeapSize=132699392 -XX:MaxGCPauseMillis=100 -XX:MaxHeapSize=268435456 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC -XX:-UseLargePagesIndividualAllocation {Heap before GC invocations=0 (full 0): garbage-first heap total 131072K, used 6144K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 6 young (6144K), 0 survivors (0K) Metaspace used 3489K, capacity 4500K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K 2020-03-05T17:30:53.626+0800: 0.412: [GC pause (G1 Evacuation Pause) (young), 0.0607668 secs] [Parallel Time: 4.0 ms, GC Workers: 4] [GC Worker Start (ms): Min: 412.5, Avg: 413.2, Max: 415.2, Diff: 2.7] [Ext Root Scanning (ms): Min: 0.0, Avg: 0.3, Max: 0.4, Diff: 0.4, Sum: 1.0] [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.0, Max: 0.0, Diff: 0.0, Sum: 0.0] [Object Copy (ms): Min: 1.1, Avg: 2.9, Max: 3.6, Diff: 2.5, Sum: 11.7] [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.2] [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.1] [GC Worker Total (ms): Min: 1.2, Avg: 3.3, Max: 3.9, Diff: 2.7, Sum: 13.0] [GC Worker End (ms): Min: 416.4, Avg: 416.4, Max: 416.4, Diff: 0.0] [Code Root Fixup: 0.0 ms] [Code Root Purge: 0.0 ms] [Clear CT: 0.0 ms] [Other: 56.8 ms] [Choose CSet: 0.0 ms] [Ref Proc: 0.1 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: 6144.0K(6144.0K)->0.0B(5120.0K) Survivors: 0.0B->1024.0K Heap: 6144.0K(128.0M)->2976.0K(128.0M)] Heap after GC invocations=1 (full 0): garbage-first heap total 131072K, used 2976K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 1 young (1024K), 1 survivors (1024K) Metaspace used 3489K, capacity 4500K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K } [Times: user=0.05 sys=0.00, real=0.06 secs] {Heap before GC invocations=1 (full 0): garbage-first heap total 131072K, used 8096K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 6 young (6144K), 1 survivors (1024K) Metaspace used 3489K, capacity 4500K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K 2020-03-05T17:30:53.824+0800: 0.554: [GC pause (G1 Evacuation Pause) (young), 0.0028500 secs] [Parallel Time: 2.6 ms, GC Workers: 4] [GC Worker Start (ms): Min: 553.6, Avg: 553.6, Max: 553.7, Diff: 0.1] [Ext Root Scanning (ms): Min: 0.1, Avg: 0.2, Max: 0.3, Diff: 0.2, Sum: 0.7] [Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0] [Processed Buffers: Min: 0, Avg: 1.0, Max: 2, Diff: 2, Sum: 4] [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.0, Max: 0.0, Diff: 0.0, Sum: 0.0] [Object Copy (ms): Min: 2.3, Avg: 2.4, Max: 2.4, Diff: 0.1, Sum: 9.5] [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0] [Termination Attempts: Min: 1, Avg: 2.5, Max: 4, Diff: 3, Sum: 10] [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.5, Avg: 2.6, Max: 2.6, Diff: 0.1, Sum: 10.3] [GC Worker End (ms): Min: 556.2, Avg: 556.2, Max: 556.2, Diff: 0.0] [Code Root Fixup: 0.0 ms] [Code Root Purge: 0.0 ms] [Clear CT: 0.0 ms] [Other: 0.2 ms] [Choose CSet: 0.0 ms] [Ref Proc: 0.1 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: 5120.0K(5120.0K)->0.0B(9216.0K) Survivors: 1024.0K->1024.0K Heap: 8096.0K(128.0M)->6077.0K(128.0M)] Heap after GC invocations=2 (full 0): garbage-first heap total 131072K, used 6077K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 1 young (1024K), 1 survivors (1024K) Metaspace used 3489K, capacity 4500K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K } [Times: user=0.06 sys=0.00, real=0.00 secs] {Heap before GC invocations=2 (full 0): garbage-first heap total 131072K, used 15293K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 10 young (10240K), 1 survivors (1024K) Metaspace used 3489K, capacity 4500K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K 2020-03-05T17:30:54.019+0800: 0.749: [GC pause (G1 Evacuation Pause) (young), 0.0025736 secs] [Parallel Time: 2.4 ms, GC Workers: 4] [GC Worker Start (ms): Min: 749.1, Avg: 749.2, Max: 749.3, Diff: 0.2] [Ext Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 0.5] [Update RS (ms): Min: 0.1, Avg: 0.3, Max: 0.5, Diff: 0.4, Sum: 1.3] [Processed Buffers: Min: 1, Avg: 1.5, Max: 3, Diff: 2, Sum: 6] [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.0, Max: 0.0, Diff: 0.0, Sum: 0.0] [Object Copy (ms): Min: 1.3, Avg: 1.7, Max: 2.0, Diff: 0.7, Sum: 6.6] [Termination (ms): Min: 0.0, Avg: 0.2, Max: 0.7, Diff: 0.7, Sum: 0.7] [Termination Attempts: Min: 1, Avg: 2.3, Max: 5, Diff: 4, Sum: 9] [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.3, Max: 2.4, Diff: 0.2, Sum: 9.3] [GC Worker End (ms): Min: 751.5, Avg: 751.5, Max: 751.5, Diff: 0.0] [Code Root Fixup: 0.0 ms] [Code Root Purge: 0.0 ms] [Clear CT: 0.0 ms] [Other: 0.2 ms] [Choose CSet: 0.0 ms] [Ref Proc: 0.1 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: 9216.0K(9216.0K)->0.0B(74.0M) Survivors: 1024.0K->2048.0K Heap: 14.9M(128.0M)->8698.0K(128.0M)] Heap after GC invocations=3 (full 0): garbage-first heap total 131072K, used 8698K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 2 young (2048K), 2 survivors (2048K) Metaspace used 3489K, capacity 4500K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K } [Times: user=0.00 sys=0.00, real=0.00 secs] {Heap before GC invocations=3 (full 0): garbage-first heap total 131072K, used 84474K [0x00000000f0000000, 0x00000000f0100400, 0x0000000100000000) region size 1024K, 76 young (77824K), 2 survivors (2048K) Metaspace used 3995K, capacity 4572K, committed 4864K, reserved 1056768K class space used 440K, capacity 460K, committed 512K, reserved 1048576K #####以下省略#####
二、GC Easy 可视化工具
GC Easy是一款在线的可视化工具,易用、功能强大
1、上传gc的log文件
2、查看报告