Search in sources :

Example 31 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project jekejeke-devel by jburse.

the class HotspotUsage method addListener.

/**
 * <p>Add a notification listener.</p>
 * <p>If this is the first listener save and init the threshold.</p>
 *
 * @param listener The listener.
 */
public final void addListener(NotificationListener listener) {
    synchronized (thresholds) {
        if (count == 0) {
            Iterator<MemoryPoolMXBean> pool = ManagementFactory.getMemoryPoolMXBeans().iterator();
            while (pool.hasNext()) {
                MemoryPoolMXBean pbean = pool.next();
                if (pbean.isUsageThresholdSupported()) {
                    long threshold = pbean.getUsageThreshold();
                    thresholds.put(pbean.getName(), Long.valueOf(threshold));
                    threshold = pbean.getUsage().getMax() * 85 / 100;
                    pbean.setUsageThreshold(threshold);
                }
            }
        }
        count++;
    }
    NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
    emitter.addNotificationListener(listener, null, null);
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 32 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project jekejeke-devel by jburse.

the class HotspotUsage method removeListener.

/**
 * <p>Remove a notificatio listener.</p>
 * <p>If this is the last listener restore the threshold.</p>
 *
 * @param listener The listener.
 */
public final void removeListener(NotificationListener listener) {
    NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
    try {
        emitter.removeNotificationListener(listener, null, null);
    } catch (ListenerNotFoundException e) {
        throw new RuntimeException(e);
    }
    synchronized (thresholds) {
        count--;
        if (count == 0) {
            Iterator<MemoryPoolMXBean> pool = ManagementFactory.getMemoryPoolMXBeans().iterator();
            while (pool.hasNext()) {
                MemoryPoolMXBean pbean = pool.next();
                if (pbean.isUsageThresholdSupported()) {
                    long threshold = thresholds.get(pbean.getName()).longValue();
                    pbean.setUsageThreshold(threshold);
                }
            }
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) ListenerNotFoundException(javax.management.ListenerNotFoundException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 33 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project OpenOLAT by OpenOLAT.

the class MemoryWebService method createMemoryPools.

private MemoryPoolVO[] createMemoryPools() {
    List<MemoryPoolMXBean> memoryPool = ManagementFactory.getMemoryPoolMXBeans();
    MemoryPoolVO[] voes = new MemoryPoolVO[memoryPool.size()];
    int count = 0;
    for (MemoryPoolMXBean bean : memoryPool) {
        if (bean.isValid()) {
            voes[count++] = new MemoryPoolVO(bean);
        }
    }
    return voes;
}
Also used : MemoryPoolVO(org.olat.restapi.system.vo.MemoryPoolVO) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 34 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project btrace by btraceio.

the class BTraceRuntime method getMemoryPoolUsage.

static String getMemoryPoolUsage(String poolFormat) {
    if (poolFormat == null) {
        poolFormat = "%1$s;%2$d;%3$d;%4$d;%5$d";
    }
    Object[][] poolOutput = new Object[memPoolList.size()][5];
    StringBuilder membuffer = new StringBuilder();
    for (int i = 0; i < memPoolList.size(); i++) {
        MemoryPoolMXBean memPool = memPoolList.get(i);
        poolOutput[i][0] = memPool.getName();
        poolOutput[i][1] = memPool.getUsage().getMax();
        poolOutput[i][2] = memPool.getUsage().getUsed();
        poolOutput[i][3] = memPool.getUsage().getCommitted();
        poolOutput[i][4] = memPool.getUsage().getInit();
    }
    for (Object[] memPoolOutput : poolOutput) {
        membuffer.append(String.format(poolFormat, memPoolOutput)).append("\n");
    }
    return membuffer.toString();
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 35 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testBadSetAttribute.

@Test
public final void testBadSetAttribute() {
    MemoryPoolMXBean testImpl = testBean;
    // Let's try and set some non-writable attributes.
    Attribute attr = new Attribute("UsageThresholdCount", new Long(25));
    try {
        mbs.setAttribute(objName, attr);
        Assert.fail("Unreacheable code: should have thrown an exception.");
    } catch (Exception e) {
        logger.debug("testBadSetAttribute exception:" + e.getClass().getName() + ", errMessage=" + e.getMessage());
        AssertJUnit.assertTrue(e instanceof AttributeNotFoundException);
    }
    // Try and set the UsageThreshold attribute with an incorrect
    // type.
    attr = new Attribute("UsageThreshold", "rubbish");
    try {
        mbs.setAttribute(objName, attr);
        Assert.fail("Unreacheable code: should have thrown an exception");
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof InvalidAttributeValueException);
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) Test(org.testng.annotations.Test)

Aggregations

MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)107 MemoryUsage (java.lang.management.MemoryUsage)43 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)16 ArrayList (java.util.ArrayList)14 Test (org.testng.annotations.Test)11 MemoryMXBean (java.lang.management.MemoryMXBean)10 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 ReflectionException (javax.management.ReflectionException)9 NotificationEmitter (javax.management.NotificationEmitter)8 MemoryType (java.lang.management.MemoryType)7 HashMap (java.util.HashMap)7 AttributeNotFoundException (javax.management.AttributeNotFoundException)7 IntrospectionException (javax.management.IntrospectionException)7 MBeanException (javax.management.MBeanException)7 Map (java.util.Map)6 Attribute (javax.management.Attribute)6 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ThreadMXBean (java.lang.management.ThreadMXBean)5 Test (org.junit.Test)5