代码之家  ›  专栏  ›  技术社区  ›  codeforester

测量GC暂停时间的最佳方法是什么?

  •  3
  • codeforester  · 技术社区  · 7 年前

    跟踪Java实例中GC暂停/暂停时间的最佳方法是什么?

    1. 可以从垃圾收集器中检索它吗 GarbageCollectorMXBean ?
    2. 可以从gc读取吗。日志

    是gc。日志有MXBean没有的其他信息吗?我更喜欢选项1,因为我希望实例向我们的监控系统发出该度量。

    我读过一些帖子,比如 this 是的,但我似乎没有得到正确的答案。我特别寻找GC暂停时间和 在GC上花费的总时间。

    2 回复  |  直到 7 年前
        1
  •  6
  •   codeforester    7 年前

    垃圾收集并不是JVM停止世界暂停的唯一原因。
    你可以数一数 other reasons

    监视safepoint暂停的第一种方法是解析VM日志:

    • 对于JDK 8和更早版本,添加 -XX:+PrintGCApplicationStoppedTime JVM选项;
    • 从JDK 9开始添加 -Xlog:safepoint .

    那就去找 Total time for which application threads were stopped 日志文件中的消息。

    第二种方法是使用 无证的 热点内部MXBean:

    sun.management.HotspotRuntimeMBean runtime =
            sun.management.ManagementFactoryHelper.getHotspotRuntimeMBean();
    
    System.out.println("Safepoint time:  " + runtime.getTotalSafepointTime() + " ms");
    System.out.println("Safepoint count: " + runtime.getSafepointCount());
    

    它给出了所有JVM暂停的累计时间。请参阅中的讨论 this answer .

        2
  •  1
  •   the8472    7 年前

    我特别寻找GC暂停时间

    要暂停的时间比GC本身要多。获取safepoint的时间也是一个应用程序暂停,只能作为部分日志记录使用,不能通过MXBeans使用

    但实际上,如果你担心应用程序暂停,那么GC暂停或超过安全点时间都不是你真正应该做的 测量 .你应该自己测量摊位,例如通过jhiccup