Search in sources :

Example 46 with MemoryPoolMXBean

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

the class StatusTransformer method writeVMState.

/**
 * Write the VM state.
 * @param writer The output writer
 * @param mode Mode <code>0</code> will generate HTML.
 *             Mode <code>1</code> will generate XML.
 * @param args I18n labels for the VM state values
 * @throws Exception Propagated JMX error
 */
public static void writeVMState(PrintWriter writer, int mode, Object[] args) throws Exception {
    SortedMap<String, MemoryPoolMXBean> memoryPoolMBeans = new TreeMap<>();
    for (MemoryPoolMXBean mbean : ManagementFactory.getMemoryPoolMXBeans()) {
        String sortKey = mbean.getType() + ":" + mbean.getName();
        memoryPoolMBeans.put(sortKey, mbean);
    }
    if (mode == 0) {
        writer.print("<h1>JVM</h1>");
        writer.print("<p>");
        writer.print(args[0]);
        writer.print(' ');
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().freeMemory()), true));
        writer.print(' ');
        writer.print(args[1]);
        writer.print(' ');
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().totalMemory()), true));
        writer.print(' ');
        writer.print(args[2]);
        writer.print(' ');
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().maxMemory()), true));
        writer.print("</p>");
        writer.write("<table border=\"0\"><thead><tr><th>" + args[3] + "</th><th>" + args[4] + "</th><th>" + args[5] + "</th><th>" + args[6] + "</th><th>" + args[7] + "</th><th>" + args[8] + "</th></tr></thead><tbody>");
        for (MemoryPoolMXBean memoryPoolMBean : memoryPoolMBeans.values()) {
            MemoryUsage usage = memoryPoolMBean.getUsage();
            writer.write("<tr><td>");
            writer.print(memoryPoolMBean.getName());
            writer.write("</td><td>");
            writer.print(memoryPoolMBean.getType());
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getInit()), true));
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getCommitted()), true));
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getMax()), true));
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getUsed()), true));
            if (usage.getMax() > 0) {
                writer.write(" (" + (usage.getUsed() * 100 / usage.getMax()) + "%)");
            }
            writer.write("</td></tr>");
        }
        writer.write("</tbody></table>");
    } else if (mode == 1) {
        writer.write("<jvm>");
        writer.write("<memory");
        writer.write(" free='" + Runtime.getRuntime().freeMemory() + "'");
        writer.write(" total='" + Runtime.getRuntime().totalMemory() + "'");
        writer.write(" max='" + Runtime.getRuntime().maxMemory() + "'/>");
        for (MemoryPoolMXBean memoryPoolMBean : memoryPoolMBeans.values()) {
            MemoryUsage usage = memoryPoolMBean.getUsage();
            writer.write("<memorypool");
            writer.write(" name='" + Escape.xml("", memoryPoolMBean.getName()) + "'");
            writer.write(" type='" + memoryPoolMBean.getType() + "'");
            writer.write(" usageInit='" + usage.getInit() + "'");
            writer.write(" usageCommitted='" + usage.getCommitted() + "'");
            writer.write(" usageMax='" + usage.getMax() + "'");
            writer.write(" usageUsed='" + usage.getUsed() + "'/>");
        }
        writer.write("</jvm>");
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) TreeMap(java.util.TreeMap) MemoryUsage(java.lang.management.MemoryUsage)

Example 47 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project openolat by klemens.

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 48 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project openolat by klemens.

the class MemoryRenderer method renderDetails.

private void renderDetails(StringOutput sb, long max, MemoryType type) {
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    long totalUsed = 0l;
    List<MemoryPoolMXBean> typedPools = new ArrayList<>(4);
    for (MemoryPoolMXBean pool : pools) {
        if (pool.getType() == type) {
            typedPools.add(pool);
            totalUsed += pool.getUsage().getUsed();
        }
    }
    String tooltip;
    if (max == -1) {
        tooltip = toMB(totalUsed) + "MB";
    } else {
        tooltip = toMB(totalUsed) + "MB / " + toMB(max) + "MB";
        totalUsed = max;
    }
    sb.append(" title=\"").append(tooltip).append("\">");
    int count = 0;
    long totalUsedPercent = 0l;
    for (MemoryPoolMXBean pool : typedPools) {
        String name = pool.getName();
        long used = pool.getUsage().getUsed();
        long usedMB = toMB(used);
        long usedPercent = toPercent(used, totalUsed);
        totalUsedPercent += usedPercent;
        if (totalUsedPercent > 100l) {
            // never more than 100%
            usedPercent = usedPercent - (totalUsedPercent - 100l);
        }
        sb.append("<div class='progress-bar ").append(bars[count++]).append("' role='progressbar' ").append(" aria-valuenow='").append(usedMB).append("'").append(" aria-valuemin='0' style='width:").append(usedPercent).append("%;'><span").append(" title=\"").append(usedMB).append("MB (").append(name).append(")\"").append(">").append(usedMB).append("MB (").append(name).append(")</span></div>");
        if (count > bars.length) {
            count = 0;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 49 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project jvm-tools by aragozin.

the class YoungGCPauseBenchmark method benchmark.

public TestResult benchmark() throws IOException {
    STRING_TEMPLATE = new char[stringLen];
    if (mode == DataMode.CONST) {
        overrideRate = 1.1d;
    }
    System.out.println("Java: " + System.getProperty("java.version") + " VM: " + System.getProperty("java.vm.version"));
    System.out.println("Data model: " + Integer.getInteger("sun.arch.data.model"));
    long tenuredSize = Runtime.getRuntime().maxMemory();
    concurentMode = false;
    // getting more accurate data
    for (MemoryPoolMXBean bean : ManagementFactory.getMemoryPoolMXBeans()) {
        if (CONC_MODE.contains(bean.getName())) {
            concurentMode = true;
        }
        if (OLD_POOLS.contains(bean.getName())) {
            tenuredSize = bean.getUsage().getMax();
            if (tenuredSize < 0) {
                tenuredSize = bean.getUsage().getCommitted();
            }
            System.out.println("Exact old space size is " + (tenuredSize) + " bytes");
            oldMemPool = bean;
            break;
        }
    }
    beans: for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
        for (String pool : bean.getMemoryPoolNames()) {
            if (OLD_POOLS.contains(pool)) {
                oldGcMBean = (com.sun.management.GarbageCollectorMXBean) bean;
                continue beans;
            } else {
            }
        }
        youngGcMBean = bean;
    }
    boolean entrySizeAdjust = false;
    if (entrySize <= 0) {
        entrySizeAdjust = true;
        System.out.println("Estimating entry memory footprint ...");
        System.gc();
        long used = getOldSpaceUsed();
        int testSize = 100000;
        initMaps(testSize);
        while (size() < testSize) {
            populateMap(concurentMode, testSize);
        }
        System.gc();
        long footPrint = getOldSpaceUsed() - used;
        entrySize = align((int) (footPrint / testSize), 32);
        System.out.println("Entry footprint: " + entrySize);
        maps = null;
    }
    System.gc();
    long oldSpaceUsed = getOldSpaceUsed();
    long freeTenured = tenuredSize - getOldSpaceUsed();
    calculateCount(freeTenured);
    if (concurentMode) {
        System.out.println("Concurent mode is enabled");
        System.out.println("Available old space: " + (freeTenured >> 20) + "MiB");
    } else {
        System.out.println("Available old space: " + (freeTenured >> 20) + "MiB (-" + headRoom + " MiB)");
    }
    if (count < 0) {
        System.out.println("Heap size is too small, increase heap size or reduce headroom");
        return null;
    }
    System.out.println("Young space collector: " + youngGcMBean.getName());
    System.out.println("Old space collector: " + oldGcMBean.getName());
    System.out.println("Populating - " + count);
    initMaps(count);
    int n = 0;
    if (entrySizeAdjust) {
        int targetSize = 4 * count / 5;
        while (size() < targetSize) {
            populateMap(concurentMode, count);
            n++;
        }
        System.gc();
        System.gc();
        long footPrint = getOldSpaceUsed() - oldSpaceUsed;
        entrySize = align((int) (footPrint / size()), 32);
        System.out.println("Adjusted entry footprint: " + entrySize);
        calculateCount(freeTenured);
    }
    while (size() < count) {
        populateMap(concurentMode, count);
        n++;
    }
    if (concurentMode) {
        while (--n > 0) {
            processMap(false);
        }
    }
    // Let's wait for at least one major collection to complete
    if (!oldGcMBean.getName().startsWith("G1")) {
        // so we have to ignore this
        if (oldGcMBean != null) {
            long c = oldGcMBean.getCollectionCount();
            while (c == oldGcMBean.getCollectionCount()) {
                processMap(false);
            }
        }
    }
    System.out.println("Size: " + size());
    if (!dryMode) {
        System.out.println("Processing ... ");
    } else {
        System.out.println("Processing ... (DRY MODE ENABLED)");
    }
    StringBuffer sb = new StringBuffer();
    sb.append("Limits:");
    if (maxTime > 0) {
        sb.append(" ").append(maxTime + " sec");
    }
    if (maxOld > 0) {
        sb.append(" ").append(maxOld + " old collections");
    }
    if (maxYoung > 0) {
        sb.append(" ").append(maxYoung + " young collections");
    }
    System.out.println(sb.toString());
    activeOverrideRate = overrideRate;
    YoungGcTimeTracker tracker = new YoungGcTimeTracker();
    tracker.init();
    // start count down here
    long startTime = System.currentTimeMillis();
    long oldC = oldGcMBean.getCollectionCount();
    long youngC = youngGcMBean.getCollectionCount();
    while (true) {
        processMap(dryMode);
        tracker.probe();
        if (maxOld > 0) {
            if (oldGcMBean.getCollectionCount() - oldC > maxOld) {
                break;
            }
        }
        if (maxYoung > 0) {
            if (youngGcMBean.getCollectionCount() - youngC > maxYoung) {
                break;
            }
        }
        if (maxTime > 0 && (System.currentTimeMillis() > (startTime + TimeUnit.SECONDS.toMillis(maxTime)))) {
            break;
        }
    }
    System.out.println("Benchmark complete");
    return tracker.result();
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 50 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Payara by payara.

the class MemoryReporter method getMemoryPoolReport.

private String getMemoryPoolReport() {
    final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder());
    final long millis = rmbean.getUptime();
    final String uptime = sm.getString("uptime", JVMInformationCollector.millis2HoursMinutesSeconds(millis));
    sb.append(uptime);
    for (final MemoryPoolMXBean m : pools) {
        final String n = m.getName();
        sb.append(sm.getString("memory.pool.name", n));
        MemoryUsage mu = m.getUsage();
        sb.append(mu2String(mu));
    }
    return (sb.toString());
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

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