static void createNotification(String notifType, String poolName, MemoryUsage usage, long count) { MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean(); if (!mbean.hasListeners()) { // if no listener is registered. return; } long timestamp = System.currentTimeMillis(); String msg = getNotifMsg(notifType); Notification notif = new Notification(notifType, mbean.getObjectName(), getNextSeqNumber(), timestamp, msg); MemoryNotificationInfo info = new MemoryNotificationInfo(poolName, usage, count); CompositeData cd = MemoryNotifInfoCompositeData.toCompositeData(info); notif.setUserData(cd); mbean.sendNotification(notif); }
public static void badNameCompositeData() throws Exception { CompositeType ct = new CompositeType("MyCompositeType", "CompositeType for MemoryNotificationInfo", badItemNames, badItemNames, validItemTypes); CompositeData cd = new CompositeDataSupport(ct, badItemNames, values); try { MemoryNotificationInfo info = MemoryNotificationInfo.from(cd); } catch (IllegalArgumentException e) { System.out.println("Expected exception: " + e.getMessage()); return; } throw new RuntimeException( "IllegalArgumentException not thrown"); }
@Override public void handleNotification(Notification notification, Object handback) { String nType = notification.getType(); String poolName = CodeCacheUtils.getPoolNameFromNotification(notification); // consider code cache events only if (CodeCacheUtils.isAvailableCodeHeapPoolName(poolName)) { Asserts.assertEQ(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, nType, "Unexpected event received: " + nType); // receiving events from available CodeCache-related beans only if (counters.get(poolName) != null) { counters.get(poolName).incrementAndGet(); lastEventTimestamp = System.currentTimeMillis(); } } }
public MemoryMonitor() { LOG.info("initializing"); this.springContextId = "Unknown"; MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); NotificationEmitter emitter = (NotificationEmitter) mbean; emitter.addNotificationListener(new NotificationListener() { public void handleNotification(Notification n, Object hb) { if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) { long maxMemory = tenuredGenPool.getUsage().getMax(); long usedMemory = tenuredGenPool.getUsage().getUsed(); for (Listener listener : listeners) { listener.memoryUsageLow(springContextId, usedMemory, maxMemory); } } } }, null, null); }
/** * In case we are running out of memory we free half of the cached down to a minimum of 25 cached tiles. */ public void handleNotification(Notification notification, Object handback) { log.trace("Memory notification: " + notification.toString()); if (!MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED.equals(notification.getType())) return; synchronized (lruTiles) { int count_half = lruTiles.getElementCount() / 2; count_half = Math.max(25, count_half); if (lruTiles.getElementCount() <= count_half) return; log.warn("memory low - freeing cached tiles: " + lruTiles.getElementCount() + " -> " + count_half); try { while (lruTiles.getElementCount() > count_half) { removeEntry(lruTiles.getLastElement()); } } catch (Exception e) { log.error("", e); } } }
@Override public void handleNotification(Notification notification, Object handback) { if(!notification.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) return; logger.info("MemoryNotifier::activate"); //--- copy the list of listener's in a thread-safe way; doesn't matter if we get an update while we're notifying this way. final List<Flushable> toNotify = new ArrayList<>(); synchronized (instance) { toNotify.addAll(listeners); } //--- spawn a thread to handle flushing; don't do multiple because threads can be heavy on the memory usage. Thread deferred = new Thread() { public void run() { for (final Flushable flushable : toNotify) { try { flushable.flush(); } catch (IOException e) { e.printStackTrace(); } } } }; deferred.start(); }
public void handleNotification(Notification notification, Object handback) { if (notification.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) { flushRequested = true; final Sorter f = this; Thread t = new Thread() { @Override public void run() { try { f.flush(); } catch (IOException e) { logger.severe(e.toString()); } } }; t.start(); } }
public MemoryMonitor() { LOG.info("initializing"); this.springContextId = "Unknown"; ManagementFactory.getThreadMXBean().setThreadContentionMonitoringEnabled(true); ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true); lowMemoryListener = new NotificationListener() { public void handleNotification(Notification n, Object hb) { if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) { Map<String, String> memoryUsageStatistics = new HashMap<String, String>(); memoryUsageStatistics.put("MemoryMXBean: " + MemoryType.HEAP, ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString()); memoryUsageStatistics.put("MemoryMXBean:" + MemoryType.NON_HEAP, ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().toString()); for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { memoryUsageStatistics.put("MemoryPoolMXBean: " + pool.getType(), pool.getUsage().toString()); } for (Listener listener : listeners) { listener.memoryUsageLow(springContextId, memoryUsageStatistics, Arrays.toString(ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads())); } } } }; ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(lowMemoryListener, null, null); }
/** * @return an instance of {@link CompositeType}for the * {@link MemoryNotificationInfo}class. */ private static CompositeType getMemoryNotificationInfoCompositeType() { if (MEMORYNOTIFICATIONINFO_COMPOSITETYPE == null) { String[] typeNames = { "poolName", "usage", "count" }; String[] typeDescs = { "poolName", "usage", "count" }; OpenType[] typeTypes = { SimpleType.STRING, getMemoryUsageCompositeType(), SimpleType.LONG }; try { MEMORYNOTIFICATIONINFO_COMPOSITETYPE = new CompositeType( MemoryNotificationInfo.class.getName(), MemoryNotificationInfo.class.getName(), typeNames, typeDescs, typeTypes); } catch (OpenDataException e) { if (ManagementUtils.VERBOSE_MODE) { e.printStackTrace(System.err); }// end if } } return MEMORYNOTIFICATIONINFO_COMPOSITETYPE; }
public void test_from() { final MemoryUsage memoryUsage = new MemoryUsage(1, 2, 3, 4); final MemoryNotificationInfo memoryNotifyInfo = new MemoryNotificationInfo("Lloyd", memoryUsage, 42); CompositeData compositeData = ManagementUtils .toMemoryNotificationInfoCompositeData(memoryNotifyInfo); MemoryNotificationInfo fromInfo = MemoryNotificationInfo .from(compositeData); assertEquals(memoryNotifyInfo.getPoolName(), fromInfo.getPoolName()); assertEquals(memoryNotifyInfo.getCount(), fromInfo.getCount()); MemoryUsage fromUsage = fromInfo.getUsage(); assertEquals(memoryUsage.getInit(), fromUsage.getInit()); assertEquals(memoryUsage.getMax(), fromUsage.getMax()); assertEquals(memoryUsage.getUsed(), fromUsage.getUsed()); assertEquals(memoryUsage.getCommitted(), fromUsage.getCommitted()); }
public void test_from_scenario3() throws Exception { String[] names = { "poolName", "usage", "count" }; Object[] values = { "TestPoolName", null, null }; OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, SimpleType.LONG }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); try { MemoryNotificationInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario4() throws Exception { // add String[] names = { "poolName", "usage", "count" }; Object[] values = { "TestPoolName", memoryCompositeData, new Long(-42) }; OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, SimpleType.LONG }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); MemoryNotificationInfo info = MemoryNotificationInfo.from(data); assertEquals(values[0], info.getPoolName()); assertEquals(values[2], info.getCount()); MemoryUsage usage = info.getUsage(); assertEquals(1, usage.getInit()); assertEquals(2, usage.getUsed()); assertEquals(3, usage.getCommitted()); assertEquals(4, usage.getMax()); }
public void test_from_scenario5() throws Exception { String[] names = { "poolName", "usage", "count" }; Object[] values = { new Long(1), memoryCompositeData, new Long(42) }; OpenType[] types = { SimpleType.LONG, memoryUsageCompositeType, SimpleType.LONG }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); try { MemoryNotificationInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
public void test_from_scenario6() throws Exception { String[] names = { "poolName", "usage", "count" }; Object[] values = { "TestPoolName", new Long(1), new Long(42) }; OpenType[] types = { SimpleType.STRING, SimpleType.LONG, SimpleType.LONG }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); try { MemoryNotificationInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
public void test_from_scenario7() throws Exception { String[] names = { "poolName", "usage", "count" }; Object[] values = { "TestPoolName", memoryCompositeData, "42" }; OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, SimpleType.STRING }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); try { MemoryNotificationInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
public void test_from_scenario11() throws Exception { String[] names = { "notPoolName", "usage", "count" }; Object[] values = { "TestNotPoolName", memoryCompositeData, new Long(42) }; OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, SimpleType.LONG }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); try { MemoryNotificationInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
public void test_from_scenario12() throws Exception { String[] names = { "poolName", "usage", "count", "extention" }; Object[] values = { "TestPoolName", memoryCompositeData, new Long(42), null }; OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, SimpleType.LONG, SimpleType.LONG }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); MemoryNotificationInfo info = MemoryNotificationInfo.from(data); assertEquals(values[0], info.getPoolName()); assertEquals(values[2], info.getCount()); MemoryUsage usage = info.getUsage(); assertEquals(1, usage.getInit()); assertEquals(2, usage.getUsed()); assertEquals(3, usage.getCommitted()); assertEquals(4, usage.getMax()); }
public void test_from_scenario13() throws Exception { String[] names = { "poolName", "usage", "count", "extention" }; Object[] values = { "TestPoolName", memoryCompositeData, new Long(42), "Extention" }; OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, SimpleType.LONG, SimpleType.STRING }; CompositeType compositeType = getCompositeType(names, types); CompositeData data = new CompositeDataSupport(compositeType, names, values); MemoryNotificationInfo info = MemoryNotificationInfo.from(data); assertEquals(values[0], info.getPoolName()); assertEquals(values[2], info.getCount()); MemoryUsage usage = info.getUsage(); assertEquals(1, usage.getInit()); assertEquals(2, usage.getUsed()); assertEquals(3, usage.getCommitted()); assertEquals(4, usage.getMax()); }
public final void testGetNotificationInfo() { MBeanNotificationInfo[] notifications = notifierBean .getNotificationInfo(); assertNotNull(notifications); assertTrue(notifications.length > 0); for (int i = 0; i < notifications.length; i++) { MBeanNotificationInfo info = notifications[i]; assertEquals(Notification.class.getName(), info.getName()); assertEquals("Memory Notification", info.getDescription()); String[] types = info.getNotifTypes(); for (int j = 0; j < types.length; j++) { String type = types[j]; assertTrue(type .equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || type .equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)); }// end for }// end for }