GC专题-查看日志

∥☆過路亽.° 提交于 2020-02-26 22:08:53

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 日志文件的输出路径

备注:
GC的日志是以替换的方式(>)写入的,而不是追加(>>),如果下次写入到同一个文件中的话,以前的GC内容会被清空。可以在程序启动前或者停止时增加如下脚本,将之前gc.log转储到其它文件中。

if [ -f ${LOGS}/gc.log ]; then
    mv ${LOGS}/gc.log ${LOGS}/gc.log.`date +%Y%m%d%H%M%S`
fi

GC日志示例

除了GC日志参数外,为了更加与生产环境一致,增加了如下几个固定参数


-Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -XX:MaxDirectMemorySize=1g -XX:SurvivorRatio=10 -XX:+UseConcMarkSweepGC

-XX:+PrintGC -Xloggc:gc.log

0.687: [GC (Allocation Failure)  873632K->751K(2009792K), 0.0024686 secs]
0.797: [GC (Allocation Failure)  874392K->805K(2009792K), 0.0016258 secs]
0.878: [GC (Allocation Failure)  874217K->765K(2009792K), 0.0014693 secs]
0.958: [GC (Allocation Failure)  874337K->703K(2009792K), 0.0015396 secs]
1.036: [GC (Allocation Failure)  874399K->852K(2009792K), 0.0022075 secs]
1.117: [GC (Allocation Failure)  874628K->772K(2009792K), 0.0015507 secs]
1.194: [GC (Allocation Failure)  874600K->788K(2009792K), 0.0014966 secs]

-XX:+PrintGC -XX:+PrintHeapAtGC -Xloggc:gc.log

{Heap before GC invocations=0 (full 0):
 par new generation   total 961216K, used 873632K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d28150, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.585: [GC (Allocation Failure)  873632K->765K(2009792K), 0.0022171 secs]
Heap after GC invocations=1 (full 0):
 par new generation   total 961216K, used 765K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=1 (full 0):
 par new generation   total 961216K, used 874406K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d2a518, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.701: [GC (Allocation Failure)  874406K->834K(2009792K), 0.0019298 secs]
Heap after GC invocations=2 (full 0):
 par new generation   total 961216K, used 834K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795e308a0, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}

-XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:gc.log

{Heap before GC invocations=0 (full 0):
 par new generation   total 961216K, used 873632K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d28150, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.793: [GC (Allocation Failure) 0.793: [ParNew: 873632K->765K(961216K), 0.0039284 secs] 873632K->765K(2009792K), 0.0041837 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Heap after GC invocations=1 (full 0):
 par new generation   total 961216K, used 765K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=1 (full 0):
 par new generation   total 961216K, used 874406K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d2a518, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.917: [GC (Allocation Failure) 0.917: [ParNew: 874406K->776K(961216K), 0.0015625 secs] 874406K->776K(2009792K), 0.0016277 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
Heap after GC invocations=2 (full 0):
 par new generation   total 961216K, used 776K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795e221c8, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}

-XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:gc.log -XX:+PrintGCDateStamps

{Heap before GC invocations=0 (full 0):
 par new generation   total 961216K, used 873632K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d28150, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3260K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
2020-02-26T14:50:12.302-0800: 0.579: [GC (Allocation Failure) 2020-02-26T14:50:12.302-0800: 0.579: [ParNew: 873632K->84744K(961216K), 0.0430026 secs] 873632K->84744K(2009792K), 0.0431113 secs] [Times: user=0.22 sys=0.05, real=0.04 secs] 
Heap after GC invocations=1 (full 0):
 par new generation   total 961216K, used 84744K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,  97% used [0x000000079b2b0000, 0x00000007a05723f0, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3260K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=1 (full 0):
 par new generation   total 961216K, used 958386K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d2a518, 0x0000000795d60000)
  from space 87360K,  97% used [0x000000079b2b0000, 0x00000007a05723f0, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
2020-02-26T14:50:12.445-0800: 0.723: [GC (Allocation Failure) 2020-02-26T14:50:12.445-0800: 0.723: [ParNew: 958386K->86052K(961216K), 0.0858331 secs] 958386K->170593K(2009792K), 0.0859161 secs] [Times: user=0.34 sys=0.08, real=0.09 secs] 
Heap after GC invocations=2 (full 0):
 par new generation   total 961216K, used 86052K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,  98% used [0x0000000795d60000, 0x000000079b169330, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 84540K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}

GC日志解读

上面日志基本上都有如下这一行,就各段进行解读下

示例
2020-02-26T14:50:12.445-0800: 0.723: [GC (Allocation Failure) 2020-02-26T14:50:12.445-0800: 0.723: [ParNew: 958386K->86052K(961216K), 0.0858331 secs] 958386K->170593K(2009792K), 0.0859161 secs] [Times: user=0.34 sys=0.08, real=0.09 secs] 


// gc发生的时间点
2020-02-26T14:50:12.445-0800: 0.723: 

// GC (分配 失败)
[GC (Allocation Failure) 2020-02-26T14:50:12.445-0800: 0.723: 

//年青代gc收集器是ParNew:年青代(eden+from+to)整体占用了(961216K),其中部分年青代(eden+from)空间回收前占用了958386K,回收后变成了86052K,耗时0.0859161 secs
[ParNew: 958386K->86052K(961216K), 0.0858331 secs] 

//整个堆回收前的占用了958386K->回收后的占用了170593K,整个堆大小(2009792K)
958386K->170593K(2009792K), 0.0859161 secs] 

//用户耗时0.34秒 , 系统耗时0.08秒 , 实际耗时 0.09秒
[Times: user=0.34 sys=0.08, real=0.09 secs] 

GC日志耗时解读

你是否对上面这段理解的我一样一知半解,下面就详细说明下

//用户耗时0.34秒 , 系统耗时0.08秒 , 实际耗时 0.09秒
[Times: user=0.34 sys=0.08, real=0.09 secs] 

官方英文说明

  • Real is wall clock time – time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like ‘user’, this is only CPU time used by the process.

中文说明

  • real —— 程序从开始到结束所用的时钟时间。这个时间包括其他进程使用的时间片和进程阻塞的时间(比如等待 I/O 完成)。

  • user —— 进程执行用户态代码(核心之外)所使用的时间。这是执行此进程所使用的实际 CPU 时间,其他进程和此进程阻塞的时间并不包括在内。在垃圾收集的情况下,表示 GC 线程执行所使用的 CPU 总时间。

  • sys —— 进程在内核态消耗的 CPU 时间,即在内核执行系统调用或等待系统事件所使用的 CPU 时间。

解读
user + sys 时间告诉我们程序执行实际使用的 CPU 时间。注意这里指所有的 CPU,因此如果在进程里有多个线程的话,这个时间可能会超过 real 所表示的时钟时间。
这也就是上面日志中 user(0.34) + sys(0.08) > real(0.09 secs)的原因。
所以我们观察单次gc时间开销主要是看real字段的耗时情况。

附:本文使用的java代码

public class GarbageCollectorDemo {
    public static void main(String[] args) {
        /* -XX:+UseParallelOldGC和-XX:+UseParallelGC结果一样,因为MXBean名字一样,但是实际使用的不一样 */
        List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gcbean : gcbeans) {
            System.out.println("alioolog gcbean:" + gcbean.getName());
        }
        List list2 = new ArrayList();
        for (int i = 0; i < 10000; i++) {
            List list = new ArrayList();
            byte[] b = new byte[1_000_000];
            list.add(b);

            if (i % 10 == 0) {
                list2.add(b);
            }
        }

        try {
            Thread.sleep(10 * 60 * 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!