跟踪 Java 实例中 GC 暂停/停止时间的最佳方法是什么?
- 可以从垃圾收集器中检索它 GarbageCollectorMXBean ?
- 可以从gc.log中读取吗?
gc.log 是否有 MXBean 没有的附加信息?我更喜欢选项 1,因为我希望实例将该指标发送到我们的监控系统。
我已经阅读了一些帖子,例如 this SO,但我似乎没有得到正确的答案。我专门寻找 GC 停顿时间,而不是 GC 所花费的总时间。
最佳答案
垃圾收集并不是 JVM stop-the-world 暂停的唯一原因。
您可能想数 other reasons ,也是。
监视安全点暂停的第一种方法是解析虚拟机日志:
- 对于 JDK 8 及更早版本,添加
-XX:+PrintGCApplicationStoppedTime
JVM 选项; - 从 JDK 9 开始添加
-Xlog:safepoint
。
然后在日志文件中查找应用程序线程停止的总时间
消息。
第二种方法是使用未记录 Hotspot 内部 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 中的讨论.
关于java - 测量 GC 暂停时间的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50380836/