Search in sources :

Example 66 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project felix by apache.

the class MemoryUsageSupport method setThreshold.

/**
 * Sets the threshold percentage.
 *
 * @param percentage The threshold as a percentage of memory consumption.
 *            This value may be 0 (zero) to switch off automatic heap dumps
 *            or in the range {@link #MIN_DUMP_THRESHOLD} to
 *            {@link #MAX_DUMP_THRESHOLD}. If set to a negative value,
 *            the default threshold is assumed.
 * @throws IllegalArgumentException if the percentage value is outside of
 *             the valid range of thresholds. The message is the percentage
 *             value which is not accepted.
 */
final void setThreshold(int percentage) {
    if (percentage < 0) {
        percentage = defaultThreshold;
    }
    if (MemoryUsageConstants.isThresholdValid(percentage)) {
        TreeSet<String> thresholdPools = new TreeSet<String>();
        TreeSet<String> noThresholdPools = new TreeSet<String>();
        List<MemoryPoolMXBean> pools = getMemoryPools();
        for (MemoryPoolMXBean pool : pools) {
            if (pool.isUsageThresholdSupported()) {
                long threshold = pool.getUsage().getMax() * percentage / 100;
                pool.setUsageThreshold(threshold);
                thresholdPools.add(pool.getName());
            } else {
                noThresholdPools.add(pool.getName());
            }
        }
        this.threshold = percentage;
        log(LogService.LOG_INFO, "Setting Automatic Memory Dump Threshold to %d%% for pools %s", threshold, thresholdPools);
        log(LogService.LOG_INFO, "Automatic Memory Dump cannot be set for pools %s", noThresholdPools);
    } else {
        throw new IllegalArgumentException(String.valueOf(percentage));
    }
}
Also used : TreeSet(java.util.TreeSet) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 67 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Lucee by lucee.

the class MacAddressWrap method getMemoryUsageAsQuery.

public static Query getMemoryUsageAsQuery(int type) throws DatabaseException {
    java.util.List<MemoryPoolMXBean> manager = ManagementFactory.getMemoryPoolMXBeans();
    Iterator<MemoryPoolMXBean> it = manager.iterator();
    Query qry = new QueryImpl(new Collection.Key[] { KeyConstants._name, KeyConstants._type, KeyConstants._used, KeyConstants._max, KeyConstants._init }, 0, "memory");
    int row = 0;
    MemoryPoolMXBean bean;
    MemoryUsage usage;
    MemoryType _type;
    while (it.hasNext()) {
        bean = it.next();
        usage = bean.getUsage();
        _type = bean.getType();
        if (type == MEMORY_TYPE_HEAP && _type != MemoryType.HEAP)
            continue;
        if (type == MEMORY_TYPE_NON_HEAP && _type != MemoryType.NON_HEAP)
            continue;
        row++;
        qry.addRow();
        qry.setAtEL(KeyConstants._name, row, bean.getName());
        qry.setAtEL(KeyConstants._type, row, _type.name());
        qry.setAtEL(KeyConstants._max, row, Caster.toDouble(usage.getMax()));
        qry.setAtEL(KeyConstants._used, row, Caster.toDouble(usage.getUsed()));
        qry.setAtEL(KeyConstants._init, row, Caster.toDouble(usage.getInit()));
    }
    return qry;
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) Collection(lucee.runtime.type.Collection) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage) MemoryType(java.lang.management.MemoryType)

Example 68 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Lucee by lucee.

the class MacAddressWrap method getPermGenSpaceBean.

public static MemoryPoolMXBean getPermGenSpaceBean() {
    java.util.List<MemoryPoolMXBean> manager = ManagementFactory.getMemoryPoolMXBeans();
    MemoryPoolMXBean bean;
    // PERM GEN
    Iterator<MemoryPoolMXBean> it = manager.iterator();
    while (it.hasNext()) {
        bean = it.next();
        if ("Perm Gen".equalsIgnoreCase(bean.getName()) || "CMS Perm Gen".equalsIgnoreCase(bean.getName())) {
            return bean;
        }
    }
    it = manager.iterator();
    while (it.hasNext()) {
        bean = it.next();
        if (StringUtil.indexOfIgnoreCase(bean.getName(), "Perm Gen") != -1 || StringUtil.indexOfIgnoreCase(bean.getName(), "PermGen") != -1) {
            return bean;
        }
    }
    // take none-heap when only one
    it = manager.iterator();
    LinkedList<MemoryPoolMXBean> beans = new LinkedList<MemoryPoolMXBean>();
    while (it.hasNext()) {
        bean = it.next();
        if (bean.getType().equals(MemoryType.NON_HEAP)) {
            beans.add(bean);
            return bean;
        }
    }
    if (beans.size() == 1)
        return beans.getFirst();
    // Class Memory/ClassBlock Memory?
    it = manager.iterator();
    while (it.hasNext()) {
        bean = it.next();
        if (StringUtil.indexOfIgnoreCase(bean.getName(), "Class Memory") != -1) {
            return bean;
        }
    }
    return null;
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) LinkedList(java.util.LinkedList)

Example 69 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Lucee by lucee.

the class MemoryControler method init.

public static synchronized void init(ConfigServer cs) {
    if (init)
        return;
    // set level
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        types.put(pool.getName(), pool.getType());
        // we should rather check for the pool name "Tenured Gen"?
        if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
            long maxMemory = pool.getUsage().getMax();
            long warningThreshold = (long) (maxMemory * 0.9);
            // long warningThreshold = maxMemory -(10*1024*1024);
            pool.setUsageThreshold(warningThreshold);
        }
    }
    MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    NotificationEmitter emitter = (NotificationEmitter) mbean;
    MemoryNotificationListener listener = new MemoryNotificationListener(types);
    emitter.addNotificationListener(listener, null, cs);
    init = true;
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 70 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Lucee by lucee.

the class MacAddressWrap method getMemoryUsageAsStruct.

public static Struct getMemoryUsageAsStruct(int type) {
    java.util.List<MemoryPoolMXBean> manager = ManagementFactory.getMemoryPoolMXBeans();
    Iterator<MemoryPoolMXBean> it = manager.iterator();
    MemoryPoolMXBean bean;
    MemoryUsage usage;
    MemoryType _type;
    long used = 0, max = 0, init = 0;
    while (it.hasNext()) {
        bean = it.next();
        usage = bean.getUsage();
        _type = bean.getType();
        if ((type == MEMORY_TYPE_HEAP && _type == MemoryType.HEAP) || (type == MEMORY_TYPE_NON_HEAP && _type == MemoryType.NON_HEAP)) {
            used += usage.getUsed();
            max += usage.getMax();
            init += usage.getInit();
        }
    }
    Struct sct = new StructImpl();
    sct.setEL(KeyConstants._used, Caster.toDouble(used));
    sct.setEL(KeyConstants._max, Caster.toDouble(max));
    sct.setEL(KeyConstants._init, Caster.toDouble(init));
    sct.setEL(KeyImpl.init("available"), Caster.toDouble(max - used));
    return sct;
}
Also used : StructImpl(lucee.runtime.type.StructImpl) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage) MemoryType(java.lang.management.MemoryType) Struct(lucee.runtime.type.Struct)

Aggregations

MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)112 MemoryUsage (java.lang.management.MemoryUsage)46 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)16 ArrayList (java.util.ArrayList)15 MemoryMXBean (java.lang.management.MemoryMXBean)12 Test (org.testng.annotations.Test)11 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 Map (java.util.Map)7 AttributeNotFoundException (javax.management.AttributeNotFoundException)7 IntrospectionException (javax.management.IntrospectionException)7 MBeanException (javax.management.MBeanException)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Attribute (javax.management.Attribute)6 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)6 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)5 ThreadMXBean (java.lang.management.ThreadMXBean)5