Search in sources :

Example 56 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project closure-compiler by google.

the class JvmMetrics method writeMemoryMetrics.

private static void writeMemoryMetrics(PrintStream out, boolean verbose, boolean pretty) {
    if (pretty) {
        out.println("\nMemory usage");
    }
    // only show overall stats in verbose mode
    if (verbose) {
        MemoryMXBean overallMemBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage usage = overallMemBean.getHeapMemoryUsage();
        writeOverallMemoryUsage(out, usage, "Heap", pretty);
        usage = overallMemBean.getNonHeapMemoryUsage();
        writeOverallMemoryUsage(out, usage, "Non-heap", pretty);
    }
    if (verbose) {
        List<MemoryPoolMXBean> mpBeans = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean mpBean : mpBeans) {
            MemoryUsage currentUsage = mpBean.getUsage();
            MemoryUsage peakUsage = mpBean.getPeakUsage();
            if (pretty) {
                out.println("\tPool " + mpBean.getName());
                writePoolMemoryUsage(out, currentUsage, peakUsage, null, true);
            } else {
                writePoolMemoryUsage(out, currentUsage, peakUsage, "mem-pool-" + normalizeName(mpBean.getName()), false);
            }
        }
    } else {
        long available = 0;
        long current = 0;
        long peak = 0;
        List<MemoryPoolMXBean> mpBeans = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean mpBean : mpBeans) {
            MemoryUsage currentUsage = mpBean.getUsage();
            available += currentUsage.getMax();
            current += currentUsage.getUsed();
            MemoryUsage peakUsage = mpBean.getPeakUsage();
            peak += peakUsage.getUsed();
        }
        MemoryUsage summaryUsage = new MemoryUsage(0, current, current, available);
        MemoryUsage summaryPeakUsage = new MemoryUsage(0, peak, peak, peak);
        if (pretty) {
            out.format("\tAggregate of %d memory pools\n", mpBeans.size());
            writePoolMemoryUsage(out, summaryUsage, summaryPeakUsage, null, true);
        } else {
            writePoolMemoryUsage(out, summaryUsage, summaryPeakUsage, "mem", false);
        }
    }
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 57 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project openj9 by eclipse.

the class TestManagementFactory method testExtMemoryMXBeanProxyNotificationEmitterRemoveListeners.

/**
 * For IBM extensions on the MemoryMXBean
 */
@Test
public void testExtMemoryMXBeanProxyNotificationEmitterRemoveListeners() {
    try {
        com.ibm.lang.management.MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", com.ibm.lang.management.MemoryMXBean.class);
        MemoryMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        MyTestListener listener = new MyTestListener();
        proxyEmitter.addNotificationListener(listener, null, null);
        // Fire off a notification and ensure that the listener receives it.
        MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
        MemoryNotificationInfo info = new MemoryNotificationInfo("Marcel", mu, 42);
        CompositeData cd = TestUtil.toCompositeData(info);
        Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Verify that the handback is as expected.
        AssertJUnit.assertNull(listener.getHandback());
        // Verify the user data of the notification.
        Notification n = listener.getNotification();
        AssertJUnit.assertNotNull(n);
        verifyMemoryNotificationUserData(n.getUserData());
        // Remove the listener
        proxyEmitter.removeNotificationListener(listener);
        // Fire off a notification and ensure that the listener does
        // *not* receive it.
        listener.resetNotificationsReceivedCount();
        notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 43);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
        AssertJUnit.assertEquals(0, listener.getNotificationsReceivedCount());
        // ListenerNotFoundException being thrown.
        try {
            proxyEmitter.removeNotificationListener(listener);
            Assert.fail("Should have thrown a ListenerNotFoundException!");
        } catch (ListenerNotFoundException e) {
            logger.debug("ListenerNotFoundException occurred, as expected: " + e.getMessage());
        }
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
        e.printStackTrace();
    } catch (ListenerNotFoundException e) {
        Assert.fail("Unexpected ListenerNotFoundException : " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) ListenerNotFoundException(javax.management.ListenerNotFoundException) Test(org.testng.annotations.Test)

Example 58 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project openj9 by eclipse.

the class TestManagementFactory method testGetMemoryMXBean.

@Test
public final void testGetMemoryMXBean() {
    MemoryMXBean mb = ManagementFactory.getMemoryMXBean();
    AssertJUnit.assertNotNull(mb);
    // Verify that there is only instance of the this bean
    MemoryMXBean mb2 = ManagementFactory.getMemoryMXBean();
    AssertJUnit.assertNotNull(mb2);
    AssertJUnit.assertSame(mb, mb2);
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) Test(org.testng.annotations.Test)

Example 59 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project openj9 by eclipse.

the class TestManagementFactory method testMemoryMXBeanProxyNotificationEmitterGetInfo.

@Test
public void testMemoryMXBeanProxyNotificationEmitterGetInfo() {
    try {
        MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", MemoryMXBean.class);
        MemoryMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        // Verify that the notification info can be retreived OK
        MBeanNotificationInfo[] notifications = proxyEmitter.getNotificationInfo();
        AssertJUnit.assertNotNull(notifications);
        AssertJUnit.assertTrue(notifications.length > 0);
        for (int i = 0; i < notifications.length; i++) {
            MBeanNotificationInfo info = notifications[i];
            AssertJUnit.assertEquals(Notification.class.getName(), info.getName());
            AssertJUnit.assertEquals("Memory Notification", info.getDescription());
            String[] types = info.getNotifTypes();
            for (int j = 0; j < types.length; j++) {
                String type = types[j];
                AssertJUnit.assertTrue(type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || type.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED));
            }
        // end for
        }
    // end for
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
    }
}
Also used : MBeanNotificationInfo(javax.management.MBeanNotificationInfo) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) IOException(java.io.IOException) Notification(javax.management.Notification) Test(org.testng.annotations.Test)

Example 60 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project openj9 by eclipse.

the class TestManagementFactory method testMemoryMXBeanProxyNotificationEmitterRemoveListeners.

@Test
public void testMemoryMXBeanProxyNotificationEmitterRemoveListeners() {
    try {
        MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", MemoryMXBean.class);
        AssertJUnit.assertNotNull(proxy);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        MyTestListener listener = new MyTestListener();
        proxyEmitter.addNotificationListener(listener, null, null);
        // Fire off a notification and ensure that the listener receives it.
        MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
        MemoryNotificationInfo info = new MemoryNotificationInfo("Marcel", mu, 42);
        CompositeData cd = TestUtil.toCompositeData(info);
        Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Verify that the handback is as expected.
        AssertJUnit.assertNull(listener.getHandback());
        // Verify the user data of the notification.
        Notification n = listener.getNotification();
        AssertJUnit.assertNotNull(n);
        verifyMemoryNotificationUserData(n.getUserData());
        // Remove the listener
        proxyEmitter.removeNotificationListener(listener);
        // Fire off a notification and ensure that the listener does
        // *not* receive it.
        listener.resetNotificationsReceivedCount();
        notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 43);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
        AssertJUnit.assertEquals(0, listener.getNotificationsReceivedCount());
        // ListenerNotFoundException being thrown.
        try {
            proxyEmitter.removeNotificationListener(listener);
            Assert.fail("Should have thrown a ListenerNotFoundException!");
        } catch (ListenerNotFoundException e) {
            logger.debug("ListenerNotFoundException occurred, as expected: " + e.getMessage());
        }
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
        e.printStackTrace();
    } catch (ListenerNotFoundException e) {
        Assert.fail("Unexpected ListenerNotFoundException : " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) ListenerNotFoundException(javax.management.ListenerNotFoundException) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Aggregations

MemoryMXBean (java.lang.management.MemoryMXBean)84 MemoryUsage (java.lang.management.MemoryUsage)30 RuntimeMXBean (java.lang.management.RuntimeMXBean)18 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)12 IOException (java.io.IOException)11 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)11 ThreadMXBean (java.lang.management.ThreadMXBean)11 NotificationEmitter (javax.management.NotificationEmitter)10 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Test (org.junit.Test)7 Test (org.testng.annotations.Test)7 ObjectName (javax.management.ObjectName)6 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 Properties (java.util.Properties)5 MalformedObjectNameException (javax.management.MalformedObjectNameException)5 Notification (javax.management.Notification)5 MemoryMXBeanImpl (com.ibm.java.lang.management.internal.MemoryMXBeanImpl)4