/** Constructs a PrintGCStat object to monitor a remote JVM. */ public PrintGCStat(MBeanServerConnection server) throws IOException { // Create the platform mxbean proxies this.rmbean = newPlatformMXBeanProxy(server, RUNTIME_MXBEAN_NAME, RuntimeMXBean.class); this.mmbean = newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME, MemoryMXBean.class); ObjectName poolName = null; ObjectName gcName = null; try { poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"); gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"); } catch (MalformedObjectNameException e) { // should not reach here assert (false); } Set<ObjectName> mbeans = server.queryNames(poolName, null); if (mbeans != null) { pools = new ArrayList<MemoryPoolMXBean>(); for (ObjectName objName : mbeans) { MemoryPoolMXBean p = newPlatformMXBeanProxy(server, objName.getCanonicalName(), MemoryPoolMXBean.class); pools.add(p); } } mbeans = server.queryNames(gcName, null); if (mbeans != null) { gcmbeans = new ArrayList<GarbageCollectorMXBean>(); for (ObjectName objName : mbeans) { GarbageCollectorMXBean gc = newPlatformMXBeanProxy( server, objName.getCanonicalName(), GarbageCollectorMXBean.class); gcmbeans.add(gc); } } }
@SuppressWarnings("unchecked") protected Object newProxyInstance( ObjectName objectName, Class interfaceClass, boolean notificationBroadcaster) throws Exception { Object jmx_proxy = MBeanServerInvocationHandler.newProxyInstance( getMBeanServerConnection(), objectName, interfaceClass, notificationBroadcaster); return addGetId(interfaceClass, jmx_proxy, objectName.getCanonicalName()); }
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"); }
/** * Return an end user readable representation of the underlying MBean * * @return the user readable description */ public String toString() { StringBuilder buf = new StringBuilder(); buf.append("MBean Name:").append("\n ").append(name.getCanonicalName()).append("\n "); if (!listAttributeDescriptions().isEmpty()) { buf.append("\nAttributes:"); for (String attrDesc : listAttributeDescriptions()) { buf.append("\n ").append(attrDesc); } } if (!listOperationDescriptions().isEmpty()) { buf.append("\nOperations:"); for (String attrDesc : listOperationDescriptions()) { buf.append("\n ").append(attrDesc); } } return buf.toString(); }
static void MonitorGC(long h) { handle = h; List<GarbageCollectorMXBean> gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans(); MBeanServer server = ManagementFactory.getPlatformMBeanServer(); try { ObjectName gcName = new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"); for (ObjectName name : server.queryNames(gcName, null)) { GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy( server, name.getCanonicalName(), GarbageCollectorMXBean.class); gcbeans.add(gc); NotificationEmitter emitter = (NotificationEmitter) gc; NotificationListener listener = new NotificationListener() { @Override public void handleNotification(Notification notification, Object handback) { if (notification .getType() .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { CompositeData ndata = (CompositeData) notification.getUserData(); GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(ndata); boolean major = "end of major GC".equals(info.getGcAction()); long free = Runtime.getRuntime().freeMemory(); long total = Runtime.getRuntime().totalMemory(); long qty = (15 * total) - (free * 100); if (qty > 0) { NotifyOSv(handle, qty); } } } }; emitter.addNotificationListener(listener, null, null); } } catch (Exception e) { throw new RuntimeException(e); } }