简体   繁体   English

如何计算GC暂停时间?

[英]How can I calculate GC pause time?

I need to find a way to get or calculate GC pause time like the time that appears when use -XX:+PrintGCDetails : 我需要找到一种获取或计算GC暂停时间的方法,例如使用-XX:+PrintGCDetails时出现的时间:

[GC [PSYoungGen: 129536K->20978K(150528K)] 129536K->92722K(492544K), 0.1067600 secs] [Times: user=0.18 sys=0.08, real=0.10 secs]
[GC [PSYoungGen: 145214K->20987K(280064K)] 216958K->213054K(622080K), 0.1758780 secs] [Times: user=0.36 sys=0.10, real=0.17 secs] 
[GC [PSYoungGen: 280059K->20976K(280064K)] 472126K->412607K(674304K), 0.1969610 secs] [Times: user=0.52 sys=0.11, real=0.20 secs] 
[Full GC [PSYoungGen: 20976K->5608K(280064K)] [ParOldGen: 391631K->393789K(841728K)] 412607K->399398K(1121792K) [PSPermGen: 4370K->4369K(21504K)], 2.0758810 secs] [Times: user=6.38 sys=0.03, real=2.08 secs] 

I have tried to use ManagementFactory.getGarbageCollectorMXBeans() List and use getCollectionTime() method but it's generate cumulative wall clock time for Scavenge GC and MarkSweep GC. 我尝试使用ManagementFactory.getGarbageCollectorMXBeans()列表并使用getCollectionTime()方法,但它会为Scavenge GC和MarkSweep GC生成累积挂钟时间。

Iterator beans = ManagementFactory.getGarbageCollectorMXBeans().iterator();
  while (beans.hasNext()) {
      GarbageCollectorMXBean bean = (GarbageCollectorMXBean) beans.next();
      System.out.println("Name: " + bean.getName());
      System.out.println("Time: " + bean.getCollectionTime() + "ms");
  }   

And the result: 结果:

Name: PS Scavenge
Time: 487ms
Name: PS MarkSweep
Time: 3993ms
Name: PS Scavenge
Time: 487ms
Name: PS MarkSweep
Time: 3993ms
Name: PS Scavenge
Time: 487ms
Name: PS MarkSweep
Time: 3993ms
Name: PS Scavenge
Time: 487ms
Name: PS MarkSweep
Time: 3993ms
Name: PS Scavenge
Time: 487ms
Name: PS MarkSweep
Time: 3993ms
Name: PS Scavenge
Time: 487ms
Name: PS MarkSweep
Time: 3993ms
.
.
.
Name: PS MarkSweep
Time: 6233ms
Name: PS Scavenge
Time: 5656ms
Name: PS MarkSweep
Time: 6233ms
Name: PS Scavenge
Time: 5656ms
Name: PS MarkSweep
Time: 6233ms
Name: PS Scavenge
Time: 5656ms
Name: PS MarkSweep
Time: 6233ms
[GC [PSYoungGen: 1163264K->931840K(1242624K)] 2989743K-    >2990439K(3625984K), 2.4818040 secs] [Times: user=19.02 sys=0.35, real=2.48 secs] 
[Full GC [PSYoungGen: 931840K->523250K(1242624K)] [ParOldGen: 2058599K->2382949K(3540480K)] 2990439K->2906199K(4783104K) [PSPermGen: 4338K->4338K(21504K)], 6.6675530 secs] [Times: user=38.70 sys=0.14, real=6.67 secs] 
Name: PS Scavenge
Time: 8137ms
Name: PS MarkSweep
Time: 12900ms
Name: PS Scavenge
Time: 8137ms

I know when GC start work it's cause Stop-the-World but I need to know the pause times that results from GC before full GC stop the world. 我知道GC启动时会导致世界停止,但我需要知道GC导致整个世界停止之前的暂停时间。

Is there a way to catch that ? 有没有办法捕捉到这一点?

There is a hotspot-specific API that provides GC notifications, including per-GC timing information: com.sun.management.GarbageCollectionNotificationInfo 有一个特定于热点的API,可提供GC通知,包括每个GC的定时信息: com.sun.management.GarbageCollectionNotificationInfo

You'll have to detect its availability to ensure compatibility with other JVMs. 您必须检测其可用性以确保与其他JVM的兼容性。

The simplest way is to use 最简单的方法是使用

-XX:+PrintGCApplicationStoppedTime

Despite the name it includes all triggered detected by the JVM. 尽管名称如此,但它包括JVM检测到的所有触发。 You can ignore the apuses which are really small and don't relate to a GC. 您可以忽略真正很小且与GC不相关的应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM