emitter.addNotificationListener(listener, null, null);
public void start() { // unsupported if null if (tenuredGenPool == null) { return; } MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean(); NotificationEmitter emitter = (NotificationEmitter) mxBean; notificationListener = (n, hb) -> { if (n.getType().equals( MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { long maxMemory = tenuredGenPool.getUsage().getMax(); long usedMemory = tenuredGenPool.getUsage().getUsed(); for (Listener listener : listeners) { listener.memoryUsageAboveThreshold(usedMemory, maxMemory); } } }; emitter.addNotificationListener(notificationListener, null, null); }
HeapUsageMonitor() { int c = 0; for( MemoryPoolMXBean m : ManagementFactory.getMemoryPoolMXBeans() ) { if( m.getType() != MemoryType.HEAP ) // only interested in HEAP continue; if( m.isCollectionUsageThresholdSupported() && m.isUsageThresholdSupported()) { // should be Old pool, get called when memory is critical _oldGenBean = m; _gc_callback = MEM_MAX; // Really idiotic API: no idea what the usageThreshold is, so I have // to guess. Start high, catch IAE & lower by 1/8th and try again. while( true ) { try { m.setCollectionUsageThreshold(_gc_callback); break; } catch( IllegalArgumentException iae ) { // Do NOT log this exception, it is expected and unavoidable and // entirely handled. _gc_callback -= (_gc_callback>>3); } } NotificationEmitter emitter = (NotificationEmitter) _allMemBean; emitter.addNotificationListener(this, null, m); ++c; } } assert c == 1; }
/** * Register with the JVM to get threshold events. * * Package private for testing. */ void startJVMThresholdListener() { final MemoryPoolMXBean memoryPoolMXBean = getTenuredMemoryPoolMXBean(); // Set collection threshold to a low value, so that we can get // notifications after every GC run. After each such collection // threshold notification we set the usage thresholds to an // appropriate value. if (!testDisableMemoryUpdates) { memoryPoolMXBean.setCollectionUsageThreshold(1); } final long usageThreshold = memoryPoolMXBean.getUsageThreshold(); this.cache.getLogger().info( String.format("Overridding MemoryPoolMXBean heap threshold bytes %s on pool %s", new Object[] {usageThreshold, memoryPoolMXBean.getName()})); MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); NotificationEmitter emitter = (NotificationEmitter) mbean; emitter.addNotificationListener(this, null, null); }
emitter.addNotificationListener(memoryListener, null, null);
emitter.addNotificationListener(me, null, null); List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p : pools) {
/** * Construct a <code>NotificationMemoryMonitor</code> instance and * register it with the Java5 memory management system. */ NotificationMemoryMonitor() { super(); final MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); final NotificationEmitter emitter = (NotificationEmitter) mbean; // register with the Java5 memory management system. emitter.addNotificationListener(new NotificationHandler(), null, null); }
public MemoryAllocationExports() { AllocationCountingNotificationListener listener = new AllocationCountingNotificationListener(allocatedCounter); for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) { ((NotificationEmitter) garbageCollectorMXBean).addNotificationListener(listener, null, null); } }
public static void addWatchDogToAllPools(final long threshold, final NotificationListener listener) { final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); final NotificationEmitter ne = (NotificationEmitter) memBean; ne.addNotificationListener(listener, null, null); final List<MemoryPoolMXBean> memPools = ManagementFactory .getMemoryPoolMXBeans(); for (final MemoryPoolMXBean mp : memPools) { if (mp.isUsageThresholdSupported()) { final MemoryUsage mu = mp.getUsage(); final long max = mu.getMax(); final long alert = (max * threshold) / 100; // LOG.info("Setting a threshold shutdown on pool: " + mp.getName() // + " for: " + alert); mp.setUsageThreshold(alert); } } }
/** * Add a new listener. * @param pListener Listener to notify * @param pFilter Notification filter * @param pHandback handback object reference * @throws java.lang.IllegalArgumentException Listener parameter is null */ public void addNotificationListener(NotificationListener pListener, NotificationFilter pFilter, java.lang.Object pHandback) throws java.lang.IllegalArgumentException { ((NotificationEmitter) (this.resource)).addNotificationListener(pListener, pFilter, pHandback); } }
@Override public void addNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback) throws IllegalArgumentException { if (_emitter != null) { _emitter.addNotificationListener(listener, filter, handback); } }
private void registerBeanListener() { for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { if (gcBean instanceof NotificationEmitter) { ((NotificationEmitter) gcBean).addNotificationListener(this, null, gcBean); } } }
public GcMemoryMetrics(final String prefix, final MetricRegistry metricRegistry) { this.prefix = prefix; this.metricRegistry = metricRegistry; ManagementFactory.getGarbageCollectorMXBeans().forEach(gc -> { final NotificationEmitter emitter = (NotificationEmitter) gc; emitter.addNotificationListener(this::listener, null, null); }); }
private void startListening() { val notificationListener = new GcNotificationListener(); for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) { if (mbean instanceof NotificationEmitter) { val emitter = (NotificationEmitter) mbean; emitter.addNotificationListener(notificationListener, null, null); } } }
public MemoryAllocationExports() { AllocationCountingNotificationListener listener = new AllocationCountingNotificationListener(allocatedCounter); for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) { ((NotificationEmitter) garbageCollectorMXBean).addNotificationListener(listener, null, null); } }
private void startListening() { val notificationListener = new GcNotificationListener(); for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) { if (mbean instanceof NotificationEmitter) { val emitter = (NotificationEmitter) mbean; emitter.addNotificationListener(notificationListener, null, null); } } }
public static void monitorMemory(double threshold) { findPSOldGenPool().forEachOrThrow(poolMxBean -> poolMxBean.setCollectionUsageThreshold((int) Math.floor(poolMxBean .getUsage().getMax() * threshold))); NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean(); emitter.addNotificationListener(notificationListener, null, null); }
public static void monitorMemory(double threshold) { findPSOldGenPool().forEachOrThrow(poolMxBean -> poolMxBean.setCollectionUsageThreshold((int) Math.floor(poolMxBean .getUsage().getMax() * threshold))); NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean(); emitter.addNotificationListener(notificationListener, null, null); }
@Override protected void triggerLeak() throws Exception { ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener( new CustomNotificationListener(), null, null); }
@Override protected void triggerLeak() throws Exception { final Class<?> listenerWrapperClass = getClassLoaderLeakPreventor().findClass("com.sun.jmx.interceptor.DefaultMBeanServerInterceptor$ListenerWrapper"); final Constructor<?> constructor = listenerWrapperClass.getDeclaredConstructors()[0]; constructor.setAccessible(true); final NotificationListener wrappedListener = (NotificationListener) constructor.newInstance( new MXBeanNotificationListenersCleanUpTest.CustomNotificationListener(), null, null); ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener( wrappedListener, null, null); } }