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);
}
}
}
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();
}
}
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);
}
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());
}
}
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();
}
}
Aggregations