Java Code Examples for com.sun.management.GarbageCollectionNotificationInfo#from()

The following examples show how to use com.sun.management.GarbageCollectionNotificationInfo#from() . 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: GarbageCollectionNotificationContentTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(listenerInvoked.get(source) == null) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 2
Source File: GarbageCollectionNotificationTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(!listenerInvoked.get(source)) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 3
Source File: GarbageCollectionNotificationContentTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(listenerInvoked.get(source) == null) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 4
Source File: GarbageCollectionNotificationTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(!listenerInvoked.get(source)) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 5
Source File: GarbageCollectionNotificationContentTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(listenerInvoked.get(source) == null) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 6
Source File: ReferenceManagerImpl.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
private NotificationListener createNotificationListener() {
  Set<String> ignoredMemoryAreas = new HashSet<>(Arrays.asList("Code Cache", "Compressed Class Space", "Metaspace"));
  return (notification, handback) -> {
      if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());

        //sum up used and max memory across relevant memory areas
        long totalMemUsed = 0;
        long totalMemMax = 0;
        for (Map.Entry<String, MemoryUsage> entry : info.getGcInfo().getMemoryUsageAfterGc().entrySet()) {
          String name = entry.getKey();
          if (!ignoredMemoryAreas.contains(name)) {
            MemoryUsage detail = entry.getValue();
            totalMemUsed += detail.getUsed();
            totalMemMax += detail.getMax();
          }
        }
        float heapUsage = (float) totalMemUsed / (float) totalMemMax;
        int heapUsagePercent = (int) Math.floor(heapUsage * 100f);
        logger.trace("heap usage after GC: " + heapUsagePercent + "%");
        maybeClearReferences(heapUsage);
      }
    };
}
 
Example 7
Source File: GarbageCollectionNotificationContentTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(listenerInvoked.get(source) == null) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 8
Source File: GarbageCollectionNotificationContentTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(listenerInvoked.get(source) == null) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 9
Source File: GarbageCollectionNotificationTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(!listenerInvoked.get(source)) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 10
Source File: GarbageCollectionNotificationTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(!listenerInvoked.get(source)) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 11
Source File: JVMUtils.java    From babar with Apache License 2.0 6 votes vote down vote up
/**
 * Register to all notifications from the GC MXBeans.
 * @param listener          A java NotificationListener
 */
public static void registerGCListener(GCListener listener) {

    // takes the GC listener and makes javax NotificationListener that filters GC notifications
    NotificationListener notificationListener = new NotificationListener() {
        @Override
        public void handleNotification(Notification notification, Object handback) {
            if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) return;

            // get the GC info object
            GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData)notification.getUserData());
            String gcAction = info.getGcAction();
            if (MINOR_GC_ACTION.equals(gcAction)) {
                listener.onMinorGc(info.getGcInfo().getDuration());
            }
            else if (MAJOR_GC_ACTION.equals(gcAction)) {
                listener.onMajorGc(info.getGcInfo().getDuration());
            }
        }
    };

    // register to the GC beans
    for (GarbageCollectorMXBean bean: ManagementFactory.getGarbageCollectorMXBeans()) {
        ((NotificationEmitter)bean).addNotificationListener(notificationListener, null, null);
    }
}
 
Example 12
Source File: GarbageCollectionNotificationTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(!listenerInvoked.get(source)) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 13
Source File: GarbageCollectionNotificationContentTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(listenerInvoked.get(source) == null) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 14
Source File: GarbageCollectionNotificationTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void handleNotification(Notification notif, Object handback) {
    String type = notif.getType();
    if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        GarbageCollectionNotificationInfo gcNotif =
            GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
        String source = ((ObjectName)notif.getSource()).getCanonicalName();
        synchronized(synchronizer) {
            if(!listenerInvoked.get(source)) {
                    listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
                    count++;
                    if(count >= number) {
                        synchronizer.notify();
                    }
            }
        }
    }
}
 
Example 15
Source File: GarbageCollectionMonitor.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleNotification(Notification notification, Object handback) {
    if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        return;
    }

    GarbageCollectionNotificationInfo data = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
    for (Listener listener : this.listeners) {
        listener.onGc(data);
    }
}
 
Example 16
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 17
Source File: TestExcessGCLockerCollections.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleNotification(Notification notification, Object handback) {
    try {
        if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
            GarbageCollectionNotificationInfo info =
                    GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());

            String gc_cause = info.getGcCause();

            if (gc_cause.equals(TestExcessGCLockerCollectionsStringConstants.GCLOCKER_CAUSE)) {
                Map<String, MemoryUsage> memory_before_gc = info.getGcInfo().getMemoryUsageBeforeGc();

                for (String newGenPoolName : newGenPoolNames) {
                    MemoryUsage usage = memory_before_gc.get(newGenPoolName);
                    if (usage == null) continue;

                    double startTime = ((double) info.getGcInfo().getStartTime()) / 1000.0;
                    long used = usage.getUsed();
                    long committed = usage.getCommitted();
                    long max = usage.getMax();
                    double used_percent = (((double) used) / Math.max(committed, max)) * 100.0;

                    System.out.printf("%6.3f: (%s) %d/%d/%d, %8.4f%% (%s)\n",
                                      startTime, gc_cause, used, committed, max, used_percent,
                                      ((used_percent < MIN_USED_PERCENT) ? TestExcessGCLockerCollectionsStringConstants.USED_TOO_LOW
                                                                         : TestExcessGCLockerCollectionsStringConstants.USED_OK));
                }
            }
        }
    } catch (RuntimeException ex) {
        System.err.println("Exception during notification processing:" + ex);
        ex.printStackTrace();
    }
}
 
Example 18
Source File: MemoryMonitor.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public void installMonitor(){
        //get all the GarbageCollectorMXBeans - there's one for each heap generation
        //so probably two - the old generation and young generation
        List<GarbageCollectorMXBean> gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans();
        //Install a notification handler for each bean
        for (GarbageCollectorMXBean gcbean : gcbeans) {
            NotificationEmitter emitter = (NotificationEmitter) gcbean;
            //use an anonymously generated listener for this example
            // - proper code should really use a named class
            NotificationListener listener = new NotificationListener() {
                //keep a count of the total time spent in GCs
                long totalGcDuration = 0;

                //implement the notifier callback handler
                @Override
                public void handleNotification(Notification notification, Object handback) {
                    //we only handle GARBAGE_COLLECTION_NOTIFICATION notifications here
                    if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
                        //get the information associated with this notification
                        GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
                        //get all the info and pretty print it
                        //Get the information about each memory space, and pretty print it
                        Map<String, MemoryUsage> membefore = info.getGcInfo().getMemoryUsageBeforeGc();
                        Map<String, MemoryUsage> mem = info.getGcInfo().getMemoryUsageAfterGc();
                        long memInit=0;
                        long memCommitted=0;
                        long memMax=0;
                        long memUsed=0;
//                        MemoryUsage before;

                        for (Map.Entry<String, MemoryUsage> entry : mem.entrySet()) {
                            String name = entry.getKey();
                            MemoryUsage memdetail = entry.getValue();
                             memInit += memdetail.getInit();
                             memCommitted += memdetail.getCommitted();
                             memMax += memdetail.getMax();
                             memUsed += memdetail.getUsed();
 //                           MemoryUsage before = membefore.get(name);
//                            System.out.print(name + (memCommitted==memMax?"(fully expanded)":"(still expandable)") +"used: "+(beforepercent/10)+"."+(beforepercent%10)+"%->"+(percent/10)+"."+(percent%10)+"%("+((memUsed/1048576)+1)+"MB) / ");
                        }
//                        System.out.println(" Mem max (max used or available?)"+memMax/100000+" mem used (before or after?)"+memUsed/100000+" mem committed? ="+memCommitted/1000000);
                        if(memMax>maxMemMax)
                            maxMemMax=memMax;
                        if(memUsed>maxMemUsed)
                            maxMemUsed=memUsed;
                        if(memCommitted>maxMemCommitted)
                            maxMemCommitted= memCommitted;
                    }
                }
            };
            //Add the listener
            emitter.addNotificationListener(listener, null, null);
        }
    }
 
Example 19
Source File: XTraceGCUtils.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static GarbageCollectionNotificationInfo getInfo(Notification notification) {
    CompositeData cd = (CompositeData) notification.getUserData();
    return GarbageCollectionNotificationInfo.from(cd);
}
 
Example 20
Source File: GarbageCollection.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private GarbageCollectionNotificationInfo getInfo(Notification notification) {
    CompositeData cd = (CompositeData) notification.getUserData();
    return GarbageCollectionNotificationInfo.from(cd);
}