聊聊如何打印GC日志排查的问题

作者:犀牛饲养员 时间:2023-01-22 22:10:56 

如何打印GC日志排查问题

在工作当中,有时候我们会需要打印GC的相关信息来定位问题。该如何做呢?

先来看个示例


public static void main(String[] args) {
       List<Integer> list0 = new ArrayList<>();
       long start0 = System.currentTimeMillis();
       for (int i = 0; i < 1000000; i++) {
           list0.add(i);
       }
       System.out.println("cost: " + (System.currentTimeMillis() - start0));

List<Integer> list1 = new ArrayList<>();
       long start1 = System.currentTimeMillis();
       for (int i = 0; i < 1000000; i++) {
           list1.add(i);
       }
       System.out.println("cost: " + (System.currentTimeMillis() - start1));
   }

直接运行,结果如下

cost: 135

cost: 85

你应该觉得奇怪,同样的往一个list插入100万条数据,为啥第一个耗时比较久?

这种时候,有经验的工程师应该能马上怀疑是GC的问题,我们可以通过在运行时添加JVM参数来打印程序运行时的GC情况。


-XX:+PrintGCDetails

如果是使用IDEA来运行,可以在 run configurations 的 VM options添加上面的参数即可。

再次运行,输出如下

[GC (Allocation Failure) [PSYoungGen: 1536K->512K(1536K)] 1884K->1234K(262144K), 0.0009030 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1302K->1017K(2560K)] 2025K->1948K(263168K), 0.0012290 secs] [Times: user=0.01 sys=0.01, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 2553K->1504K(3072K)] 3484K->3051K(263680K), 0.0017210 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 3040K->2033K(4608K)] 4587K->4228K(265216K), 0.0023170 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4593K->2560K(5120K)] 8195K->6681K(265728K), 0.0249700 secs] [Times: user=0.31 sys=0.00, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 5120K->3584K(6656K)] 11352K->11408K(267264K), 0.0241680 secs] [Times: user=0.30 sys=0.01, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 6656K->3296K(7680K)] 17645K->17909K(268288K), 0.0349010 secs] [Times: user=0.45 sys=0.00, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 6368K->3328K(10240K)] 25729K->25825K(270848K), 0.0314260 secs] [Times: user=0.40 sys=0.00, real=0.03 secs]
cost: 137
[GC (Allocation Failure) [PSYoungGen: 8166K->4469K(10240K)] 30663K->30103K(270848K), 0.0285370 secs] [Times: user=0.36 sys=0.01, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 9166K->3785K(13312K)] 34800K->33427K(273920K), 0.0226780 secs] [Times: user=0.28 sys=0.01, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 11465K->6142K(13824K)] 41107K->38511K(274432K), 0.0065540 secs] [Times: user=0.08 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 13822K->7837K(19456K)] 46191K->44246K(280064K), 0.0099720 secs] [Times: user=0.11 sys=0.01, real=0.01 secs]
cost: 79

所以你大概明白了,第一次minor gc的次数比第二次多,所以运行时间比较长。

那么上面打印出来的日志,具体是什么意思呢?

已经有人画出了很详细的图,我借来用下

聊聊如何打印GC日志排查的问题

聊聊如何打印GC日志排查的问题

如果想了解 GC 的更多的内容,可以看看 oracle 官方对于 HosSpotJVM GC的详细介绍

小结一下

了解GC日志的打印方法,并且能看懂GC日志,对于查找定位问题非常有帮助的。

我自己就遇到过生产上一个CPU飙到好几百的情况,打印GC日志发现 JVM 一直在 fullGC,而且每次GC之后内存基本没有变化,从而定位到应用程序可能存在内存泄漏的问题。

gc日志打印时间戳

今天发现在jvm参数中,用-XX:+PrintGCDateStamps 替换 -XX:PrintGCTimeStamps,就可以打印真实的gc时间信息,而不像现在相对启动时间的秒数。

有了真实时间戳,可以方便与应用的log做对比,便于分析排查问题。

线下实验过,真实有效gc日志打印时间戳

来源:https://blog.csdn.net/pony_maggie/article/details/102758059

标签:打印,GC日志,排查
0
投稿

猜你喜欢

  • C#基础教程之IComparable用法,实现List<T>.sort()排序

    2023-11-03 07:07:46
  • Android编程设计模式之备忘录模式详解

    2023-06-29 14:55:15
  • Java C++题解leetcode904水果成篮

    2023-12-08 21:06:09
  • 使用c#+IMap实现收取163邮件

    2023-07-21 22:44:31
  • UnityShader3实现彩光效果

    2021-09-02 03:48:53
  • C#如何自动识别文件的编码

    2022-10-08 01:41:43
  • 使用Spring Cache设置缓存条件操作

    2023-01-25 16:38:06
  • Unity实现聊天室功能

    2023-12-20 19:53:34
  • C#获取存储过程返回值和输出参数值的方法

    2021-10-19 14:45:22
  • Android仿微信键盘切换效果

    2022-05-01 08:06:41
  • struts2实现文件上传显示进度条效果

    2021-11-19 16:16:21
  • java图形用户界面实现菜单功能

    2023-11-23 11:50:58
  • Java中继承thread类与实现Runnable接口的比较

    2022-06-09 12:57:09
  • Android实现完整游戏循环的方法

    2023-12-26 16:40:06
  • springboot配置https访问的方法

    2022-12-11 16:17:37
  • AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比较

    2023-11-05 12:17:12
  • C#中List集合使用Max()方法查找到最大值的实例

    2023-12-01 08:03:32
  • WPF的ListView控件自定义布局用法实例

    2021-09-04 12:15:45
  • android 多点触摸图片缩放的具体实现方法

    2023-03-14 05:49:31
  • c++野指针的原理以及避免方法

    2023-10-07 09:18:53
  • asp之家 软件编程 m.aspxhome.com