Search in sources :

Example 1 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project elasticsearch by elastic.

the class JvmStats method jvmStats.

public static JvmStats jvmStats() {
    MemoryUsage memUsage = memoryMXBean.getHeapMemoryUsage();
    long heapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    long heapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    long heapMax = memUsage.getMax() < 0 ? 0 : memUsage.getMax();
    memUsage = memoryMXBean.getNonHeapMemoryUsage();
    long nonHeapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    long nonHeapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    List<MemoryPool> pools = new ArrayList<>();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        try {
            MemoryUsage usage = memoryPoolMXBean.getUsage();
            MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
            String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
            if (name == null) {
                // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
                continue;
            }
            pools.add(new MemoryPool(name, usage.getUsed() < 0 ? 0 : usage.getUsed(), usage.getMax() < 0 ? 0 : usage.getMax(), peakUsage.getUsed() < 0 ? 0 : peakUsage.getUsed(), peakUsage.getMax() < 0 ? 0 : peakUsage.getMax()));
        } catch (Exception ex) {
        /* ignore some JVMs might barf here with:
                 * java.lang.InternalError: Memory Pool not found
                 * we just omit the pool in that case!*/
        }
    }
    Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, Collections.unmodifiableList(pools));
    Threads threads = new Threads(threadMXBean.getThreadCount(), threadMXBean.getPeakThreadCount());
    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    GarbageCollector[] collectors = new GarbageCollector[gcMxBeans.size()];
    for (int i = 0; i < collectors.length; i++) {
        GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
        collectors[i] = new GarbageCollector(GcNames.getByGcName(gcMxBean.getName(), gcMxBean.getName()), gcMxBean.getCollectionCount(), gcMxBean.getCollectionTime());
    }
    GarbageCollectors garbageCollectors = new GarbageCollectors(collectors);
    List<BufferPool> bufferPoolsList = Collections.emptyList();
    try {
        List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
        bufferPoolsList = new ArrayList<>(bufferPools.size());
        for (BufferPoolMXBean bufferPool : bufferPools) {
            bufferPoolsList.add(new BufferPool(bufferPool.getName(), bufferPool.getCount(), bufferPool.getTotalCapacity(), bufferPool.getMemoryUsed()));
        }
    } catch (Exception e) {
    // buffer pools are not available
    }
    Classes classes = new Classes(classLoadingMXBean.getLoadedClassCount(), classLoadingMXBean.getTotalLoadedClassCount(), classLoadingMXBean.getUnloadedClassCount());
    return new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime(), mem, threads, garbageCollectors, bufferPoolsList, classes);
}
Also used : ArrayList(java.util.ArrayList) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryUsage(java.lang.management.MemoryUsage) IOException(java.io.IOException) BufferPoolMXBean(java.lang.management.BufferPoolMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 2 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.
     * @throws Exception Propagated JMX error
     */
public static void writeVMState(PrintWriter writer, int mode) 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(" Free memory: ");
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().freeMemory()), true));
        writer.print(" Total memory: ");
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().totalMemory()), true));
        writer.print(" Max memory: ");
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().maxMemory()), true));
        writer.print("</p>");
        writer.write("<table border=\"0\"><thead><tr><th>Memory Pool</th><th>Type</th><th>Initial</th><th>Total</th><th>Maximum</th><th>Used</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='" + 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 3 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project buck by facebook.

the class PerfStatsTracking method probeMemory.

public void probeMemory() {
    long freeMemoryBytes = Runtime.getRuntime().freeMemory();
    long totalMemoryBytes = Runtime.getRuntime().totalMemory();
    long totalGcTimeMs = 0;
    for (GarbageCollectorMXBean gcMxBean : ManagementFactory.getGarbageCollectorMXBeans()) {
        long collectionTimeMs = gcMxBean.getCollectionTime();
        if (collectionTimeMs == -1) {
            // Gc collection time is not supported on this JVM.
            totalGcTimeMs = -1;
            break;
        }
        totalGcTimeMs += collectionTimeMs;
    }
    ImmutableMap.Builder<String, Long> currentMemoryBytesUsageByPool = ImmutableMap.builder();
    for (MemoryPoolMXBean memoryPoolBean : ManagementFactory.getMemoryPoolMXBeans()) {
        String name = memoryPoolBean.getName();
        MemoryType type = memoryPoolBean.getType();
        long currentlyUsedBytes = memoryPoolBean.getUsage().getUsed();
        currentMemoryBytesUsageByPool.put(name + "(" + type + ")", currentlyUsedBytes);
    }
    eventBus.post(new MemoryPerfStatsEvent(freeMemoryBytes, totalMemoryBytes, totalGcTimeMs, currentMemoryBytesUsageByPool.build()));
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) ImmutableMap(com.google.common.collect.ImmutableMap) MemoryType(java.lang.management.MemoryType)

Example 4 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project jdk8u_jdk by JetBrains.

the class MXBeanInteropTest1 method doMemoryPoolMXBeanTest.

private final int doMemoryPoolMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0;
    System.out.println("---- MemoryPoolMXBean");
    try {
        ObjectName filterName = new ObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
        Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
        for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
            ObjectName memoryPoolName = iter.next();
            System.out.println("-------- " + memoryPoolName);
            MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryPoolName);
            errorCount += checkNonEmpty(mbInfo);
            System.out.println("getMBeanInfo\t\t" + mbInfo);
            MemoryPoolMXBean memoryPool = null;
            memoryPool = JMX.newMXBeanProxy(mbsc, memoryPoolName, MemoryPoolMXBean.class, true);
            System.out.println("getCollectionUsage\t\t" + memoryPool.getCollectionUsage());
            System.out.println("getMemoryManagerNames\t\t" + Arrays.deepToString(memoryPool.getMemoryManagerNames()));
            System.out.println("getName\t\t" + memoryPool.getName());
            System.out.println("getPeakUsage\t\t" + memoryPool.getPeakUsage());
            System.out.println("getType\t\t" + memoryPool.getType());
            System.out.println("getUsage\t\t" + memoryPool.getUsage());
            System.out.println("isValid\t\t" + memoryPool.isValid());
            boolean supported = memoryPool.isUsageThresholdSupported();
            System.out.println("isUsageThresholdSupported\t\t" + supported);
            if (supported) {
                System.out.println("getUsageThreshold\t\t" + memoryPool.getUsageThreshold());
                System.out.println("isUsageThresholdExceeded\t\t" + memoryPool.isUsageThresholdExceeded());
                System.out.println("getUsageThresholdCount\t\t" + memoryPool.getUsageThresholdCount());
            }
            supported = memoryPool.isCollectionUsageThresholdSupported();
            System.out.println("isCollectionUsageThresholdSupported\t\t" + supported);
            if (supported) {
                System.out.println("getCollectionUsageThreshold\t\t" + memoryPool.getCollectionUsageThreshold());
                System.out.println("getCollectionUsageThresholdCount\t\t" + memoryPool.getCollectionUsageThresholdCount());
                System.out.println("isCollectionUsageThresholdExceeded\t\t" + memoryPool.isCollectionUsageThresholdExceeded());
            }
            memoryPool.resetPeakUsage();
        }
        System.out.println("---- OK\n");
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        errorCount++;
        System.out.println("---- ERROR\n");
    }
    return errorCount;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) ObjectName(javax.management.ObjectName)

Example 5 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project heron by twitter.

the class JVMMetrics method updateMemoryPoolMetrics.

// Gather metrics for different memory pools in heap, for instance:
// Par Eden Space, Par Survivor Space, CMS Old Gen, CMS Perm Gen
private void updateMemoryPoolMetrics() {
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeanList) {
        String normalizedKeyName = memoryPoolMXBean.getName().replaceAll("[^\\w]", "-");
        MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
        if (peakUsage != null) {
            jvmPeakUsagePerMemoryPool.safeScope(normalizedKeyName + "-used").setValue(peakUsage.getUsed() / Constants.MB_TO_BYTES);
            jvmPeakUsagePerMemoryPool.safeScope(normalizedKeyName + "-committed").setValue(peakUsage.getCommitted() / Constants.MB_TO_BYTES);
            jvmPeakUsagePerMemoryPool.safeScope(normalizedKeyName + "-max").setValue(peakUsage.getMax() / Constants.MB_TO_BYTES);
        }
        MemoryUsage collectionUsage = memoryPoolMXBean.getCollectionUsage();
        if (collectionUsage != null) {
            jvmCollectionUsagePerMemoryPool.safeScope(normalizedKeyName + "-used").setValue(collectionUsage.getUsed() / Constants.MB_TO_BYTES);
            jvmCollectionUsagePerMemoryPool.safeScope(normalizedKeyName + "-committed").setValue(collectionUsage.getCommitted() / Constants.MB_TO_BYTES);
            jvmCollectionUsagePerMemoryPool.safeScope(normalizedKeyName + "-max").setValue(collectionUsage.getMax() / Constants.MB_TO_BYTES);
        }
        MemoryUsage estimatedUsage = memoryPoolMXBean.getUsage();
        if (estimatedUsage != null) {
            jvmEstimatedUsagePerMemoryPool.safeScope(normalizedKeyName + "-used").setValue(estimatedUsage.getUsed() / Constants.MB_TO_BYTES);
            jvmEstimatedUsagePerMemoryPool.safeScope(normalizedKeyName + "-committed").setValue(estimatedUsage.getCommitted() / Constants.MB_TO_BYTES);
            jvmEstimatedUsagePerMemoryPool.safeScope(normalizedKeyName + "-max").setValue(estimatedUsage.getMax() / Constants.MB_TO_BYTES);
        }
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Aggregations

MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)108 MemoryUsage (java.lang.management.MemoryUsage)44 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 Map (java.util.Map)7 AttributeNotFoundException (javax.management.AttributeNotFoundException)7 IntrospectionException (javax.management.IntrospectionException)7 MBeanException (javax.management.MBeanException)7 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