Java Code Examples for com.sun.management.GcInfo#getDuration()

The following examples show how to use com.sun.management.GcInfo#getDuration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JvmHeapPressureMetrics.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private void monitor() {
    double maxOldGen = JvmMemory.getOldGen().map(mem -> JvmMemory.getUsageValue(mem, MemoryUsage::getMax)).orElse(0.0);

    for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) {
        if (!(mbean instanceof NotificationEmitter)) {
            continue;
        }
        NotificationListener notificationListener = (notification, ref) -> {
            if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
                return;
            }

            CompositeData cd = (CompositeData) notification.getUserData();
            GarbageCollectionNotificationInfo notificationInfo = GarbageCollectionNotificationInfo.from(cd);

            String gcCause = notificationInfo.getGcCause();
            GcInfo gcInfo = notificationInfo.getGcInfo();
            long duration = gcInfo.getDuration();

            if (!JvmMemory.isConcurrentPhase(gcCause)) {
                gcPauseSum.record(duration);
            }

            Map<String, MemoryUsage> after = gcInfo.getMemoryUsageAfterGc();

            if (oldGenPoolName != null) {
                final long oldAfter = after.get(oldGenPoolName).getUsed();
                lastOldGenUsageAfterGc.set(oldAfter / maxOldGen);
            }
        };
        NotificationEmitter notificationEmitter = (NotificationEmitter) mbean;
        notificationEmitter.addNotificationListener(notificationListener, null, null);
        notificationListenerCleanUpRunnables.add(() -> {
            try {
                notificationEmitter.removeNotificationListener(notificationListener);
            } catch (ListenerNotFoundException ignore) {
            }
        });
    }
}
 
Example 2
Source File: GcEvent.java    From kanela with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    final GcInfo gcInfo = info.getGcInfo();
    final long totalBefore = getTotalUsage(gcInfo.getMemoryUsageBeforeGc());
    final long totalAfter = getTotalUsage(gcInfo.getMemoryUsageAfterGc());
    final long max = getTotalMaxUsage(gcInfo.getMemoryUsageAfterGc());
    final String  name = info.getGcName();

    String unit = "KiB";
    double cnv = ONE_KIBIBYTE;
    if (max > ONE_GIBIBYTE) {
        unit = "GiB";
        cnv = ONE_GIBIBYTE;
    } else if (max > ONE_MEBIBYTE) {
        unit = "MiB";
        cnv = ONE_MEBIBYTE;
    }

    final Date d = new Date(startTime);
    final String change = format("%.1f%s => %.1f%s / %.1f%s", totalBefore / cnv, unit, totalAfter / cnv, unit, max / cnv, unit);
    final String percentChange = format("%.1f%% => %.1f%%", 100.0 * totalBefore / max, 100.0 * totalAfter / max);

    return "OLD" + ": "
            + name + ", id=" + gcInfo.getId() + ", at=" + d.toString()
            + ", duration=" + gcInfo.getDuration() + "ms" + ", cause=[" + info.getGcCause() + "]"
            + ", " + change + " (" + percentChange + ")";
}
 
Example 3
Source File: GcEvent.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  final GcInfo gcInfo = info.getGcInfo();
  final long totalBefore = HelperFunctions.getTotalUsage(gcInfo.getMemoryUsageBeforeGc());
  final long totalAfter = HelperFunctions.getTotalUsage(gcInfo.getMemoryUsageAfterGc());
  final long max = HelperFunctions.getTotalMaxUsage(gcInfo.getMemoryUsageAfterGc());

  String unit = "KiB";
  double cnv = ONE_KIBIBYTE;
  if (max > ONE_GIBIBYTE) {
    unit = "GiB";
    cnv = ONE_GIBIBYTE;
  } else if (max > ONE_MEBIBYTE) {
    unit = "MiB";
    cnv = ONE_MEBIBYTE;
  }

  String change = String.format(
    "%.1f%s => %.1f%s / %.1f%s",
    totalBefore / cnv, unit,
    totalAfter / cnv, unit,
    max / cnv, unit);
  String percentChange = String.format(
    "%.1f%% => %.1f%%", 100.0 * totalBefore / max, 100.0 * totalAfter / max);

  final Date d = new Date(startTime);
  return type.toString() + ": "
      + name + ", id=" + gcInfo.getId() + ", at=" + d.toString()
      + ", duration=" + gcInfo.getDuration() + "ms" + ", cause=[" + info.getGcCause() + "]"
      + ", " + change + " (" + percentChange + ")";
}
 
Example 4
Source File: GCCollectionEvent.java    From buck with Apache License 2.0 5 votes vote down vote up
GCCollectionEvent(EventKey eventKey, GcInfo info) {
  super(eventKey);
  this.id = info.getId();
  this.durationInMillis = info.getDuration();
  this.memoryUsageBeforeGC = memoryUsageMap(info.getMemoryUsageBeforeGc());
  this.memoryUsageAfterGC = memoryUsageMap(info.getMemoryUsageAfterGc());
}
 
Example 5
Source File: RingBufferGarbageCollectionLog.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void handleNotification(final Notification notification, final Object handback) {
    if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        return;
    }

    final CompositeData compositeData = (CompositeData) notification.getUserData();
    final GarbageCollectionNotificationInfo gcNotification = GarbageCollectionNotificationInfo.from(compositeData);
    final GcInfo gcInfo = gcNotification.getGcInfo();

    final String gcName = gcNotification.getGcName();
    final String action = gcNotification.getGcAction();
    final String cause = gcNotification.getGcCause();

    final long startTime = jvmStartTime + gcInfo.getStartTime();
    final long endTime = jvmStartTime + gcInfo.getEndTime();

    final Map<String, MemoryUsage> usageAfter = gcInfo.getMemoryUsageAfterGc();
    final Map<String, MemoryUsage> usageBefore = gcInfo.getMemoryUsageBeforeGc();

    final List<GarbageCollectionEvent.GarbageCollectionHeapSize> heapSizes = new ArrayList<>();
    for (final Map.Entry<String, MemoryUsage> entry : usageAfter.entrySet()) {
        final MemoryUsage before = usageBefore.get(entry.getKey());
        if (before == null) {
            continue;
        }

        final MemoryUsage after = entry.getValue();
        if (after.getUsed() == before.getUsed()) {
            continue;
        }

        heapSizes.add(new StandardGarbageCollectionEvent.StandardGarbageCollectionHeapSize(entry.getKey(), before.getUsed(), after.getUsed()));
    }

    final GarbageCollectionEvent event = new StandardGarbageCollectionEvent(gcName, action, cause, startTime, endTime, heapSizes);

    if (gcInfo.getDuration() >= minDurationThreshold) {
        events.add(event);
    }

    synchronized (this) {
        final Tuple<Long, Long> previousTuple = timeAndCountPerAction.get(action);
        if (previousTuple == null){
            timeAndCountPerAction.put(action, new Tuple<>(gcInfo.getDuration(), 1L));
        } else {
            timeAndCountPerAction.put(action, new Tuple<>(gcInfo.getDuration() + previousTuple.getKey(), 1L + previousTuple.getValue()));
        }

        if (maxDurationEvent == null || event.getDuration() > maxDurationEvent.getDuration()) {
            maxDurationEvent = event;
        }
    }
}