Search code examples
garbage-collectionjvmjvm-hotspot

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?


Solution

  • 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()
          }
        }
      }