I need to find a way to get or calculate GC pause time like the time that appears when use -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.
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.
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
You'll have to detect its availability to ensure compatibility with other JVMs.
The simplest way is to use
-XX:+PrintGCApplicationStoppedTime
Despite the name it includes all triggered detected by the JVM. You can ignore the apuses which are really small and don't relate to a GC.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.