PrintGCApplicationStoppedTime

我有JVM选项-XX:+ PrintGCApplicationStoppedTime打印真正的“GC停止世界时间”。

例:

  • 应用程序线程停止的总时间:0.0018219秒
  • 应用程序线程停止的总时间:0.0016542秒
  • 应用程序线程停止的总时间:0.0015797秒

我需要通过java代码计算“GC停止世界”。 我该怎么做 ?

请注意, PrintGCApplicationStoppedTime打印安全点时间而不是GC时间
请参阅此答案了解详情。

您可以通过JDK特定(即不受支持的)MXBean获得安全点的累积时间。 此值等于Total time for which application threads were stopped消息的Total time for which application threads were stopped打印的所有数字的总和。

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