public static void main(String[] args) throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); final Boolean isNotificationSupported = AccessController.doPrivileged( new PrivilegedAction<Boolean>() { public Boolean run() { try { Class cl = Class.forName("sun.management.VMManagementImpl"); Field f = cl.getDeclaredField("gcNotificationSupport"); f.setAccessible(true); return f.getBoolean(null); } catch (ClassNotFoundException e) { return false; } catch (NoSuchFieldException e) { return false; } catch (IllegalAccessException e) { return false; } } }); if (!isNotificationSupported) { System.out.println("GC Notification not supported by the JVM, test skipped"); return; } final ObjectName gcMXBeanPattern = new ObjectName("java.lang:type=GarbageCollector,*"); Set<ObjectName> names = mbs.queryNames(gcMXBeanPattern, null); if (names.isEmpty()) throw new Exception("Test incorrect: no GC MXBeans"); number = names.size(); for (ObjectName n : names) { if (mbs.isInstanceOf(n, "javax.management.NotificationEmitter")) { listenerInvoked.put(n.getCanonicalName(), null); GcListener listener = new GcListener(); mbs.addNotificationListener(n, listener, null, null); } } // Invocation of System.gc() to trigger major GC System.gc(); // Allocation of many short living and small objects to trigger minor GC Object data[] = new Object[32]; for (int i = 0; i < 100000000; i++) { data[i % 32] = new int[8]; } int wakeup = 0; synchronized (synchronizer) { while (count != number) { synchronizer.wait(10000); wakeup++; if (wakeup > 10) break; } } for (GarbageCollectionNotificationInfo notif : listenerInvoked.values()) { checkGarbageCollectionNotificationInfoContent(notif); } System.out.println("Test passed"); }
public static void main(String[] argv) throws Exception { memory = new ObjectName(MEMORY_MXBEAN_NAME); runtime = new ObjectName(RUNTIME_MXBEAN_NAME); thread = new ObjectName(THREAD_MXBEAN_NAME); os = new ObjectName(OPERATING_SYSTEM_MXBEAN_NAME); List<MemoryPoolMXBean> pools = getMemoryPoolMXBeans(); for (MemoryPoolMXBean p : pools) { if (heapPool == null && p.getType() == MemoryType.HEAP && p.isUsageThresholdSupported() && p.isCollectionUsageThresholdSupported()) { heapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName()); } if (nonHeapPool == null && p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) { nonHeapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName()); } } // Check notification emitters MyListener listener = new MyListener(); server.addNotificationListener(memory, listener, null, null); server.removeNotificationListener(memory, listener); checkEnum(); checkList(); checkMap(); checkMemoryUsage(); checkThreadInfo(); checkOS(); checkSunGC(); System.out.println("Test passed."); }