Jakub Kubrynski
Jakub Kubrynski

Reputation: 14149

GarbageCollectionNotificationInfo values look invalid

I'm using GarbageCollectionNotificationInfo notifications to track GC events. It's nice, but looks like the output is invalid. I expect that getGcInfo().getMemoryUsageBeforeGc() -> MemoryUsage.getUsed() will report particular segment usage before running current GC. But it is always equal to getGcInfo().getMemoryUsageAfterGc() from previous notification. What's wrong here?

Upvotes: 1

Views: 827

Answers (1)

Nikem
Nikem

Reputation: 5784

Here is the I use and it is working :) I mean I get correct numbers both before and after GC.

public synchronized void handleNotification(Notification notification, Object handback) {
    if (GARBAGE_COLLECTION_NOTIFICATION.equals(notification.getType())) {
      GarbageCollectionNotificationInfo info = from((CompositeData) notification.getUserData());

      com.sun.management.GarbageCollectorMXBean mxBean = (com.sun.management.GarbageCollectorMXBean) handback;
      GcInfo gcInfo = mxBean.getLastGcInfo();

      if (gcInfo != null) {
//use gcInfo.getMemoryUsageBeforeGc() and gcInfo.getMemoryUsageAfterGc()
      }
    }
  }

Upvotes: 1

Related Questions