Search in sources :

Example 56 with MemoryUsage

use of java.lang.management.MemoryUsage in project zm-mailbox by Zimbra.

the class MemoryStats method getStatData.

/* (non-Javadoc)
     * @see com.zimbra.common.stats.RealtimeStatsCallback#getStatData()
     */
public Map<String, Object> getStatData() {
    Map<String, Object> toRet = new HashMap<String, Object>();
    // GC times
    long minorCount = 0, minorTime = 0, majorCount = 0, majorTime = 0;
    List<GarbageCollectorMXBean> gcs = ManagementFactory.getGarbageCollectorMXBeans();
    for (GarbageCollectorMXBean gc : gcs) {
        String gcName = gc.getName();
        long count = gc.getCollectionCount();
        long time = gc.getCollectionTime();
        String gcNameLower = gcName.toLowerCase();
        toRet.put(getGCCountColName(gcNameLower), count);
        toRet.put(getGCTimeColName(gcNameLower), time);
        if (sMajorCollectors.contains(gcName)) {
            majorCount += count;
            majorTime += time;
        } else {
            minorCount += count;
            minorTime += time;
        }
    }
    toRet.put(GC_MINOR_COUNT, minorCount);
    toRet.put(GC_MINOR_TIME, minorTime);
    toRet.put(GC_MAJOR_COUNT, majorCount);
    toRet.put(GC_MAJOR_TIME, majorTime);
    long heapTotal = 0;
    long heapUsed = 0;
    // mempool stats
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean pool : pools) {
        // for bug 16398, include non-heap pools (so we get permgen info)            
        //            if (pool.getType() != MemoryType.HEAP) {
        //                continue;
        //            }
        String poolName = pool.getName().toLowerCase();
        long committed;
        long used;
        try {
            MemoryUsage usage = pool.getUsage();
            committed = usage.getCommitted();
            used = usage.getUsed();
        } catch (IllegalArgumentException e) {
            // bug 31685, on java 1.5 we're getting an exception in the MemoryPoolMXBean:
            // java.lang.IllegalArgumentException: committed = 494993408 should be < max = 494927872
            // try to parse out the committed and max values if they match the string that we know.
            String msg = e.getMessage();
            Pattern p = Pattern.compile("committed = (\\d+) should be < max = (\\d+)");
            Matcher m = p.matcher(msg);
            if (m.find()) {
                committed = Long.parseLong(m.group(1));
                used = Long.parseLong(m.group(2));
            } else {
                committed = 0;
                used = 0;
            }
        }
        if (pool.getType() == MemoryType.HEAP) {
            heapTotal += committed;
            heapUsed += used;
        }
        long curpoolFree = committed - used;
        toRet.put(getPoolUsedSizeColName(poolName), used);
        toRet.put(getPoolFreeSizeColName(poolName), curpoolFree);
    }
    // heap stats
    toRet.put(HEAP_USED, heapUsed);
    toRet.put(HEAP_FREE, heapTotal - heapUsed);
    return toRet;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryUsage(java.lang.management.MemoryUsage) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 57 with MemoryUsage

use of java.lang.management.MemoryUsage in project zm-mailbox by Zimbra.

the class MemoryStats method dumpMemoryPools.

/**
     * Output human-readable stats about the memory pools in the system
     * 
     * @return
     */
public static String dumpMemoryPools() {
    StringBuilder sb = new StringBuilder();
    long totalUsed = 0;
    long totalReserved = 0;
    long totalMax = 0;
    long collectUsed = 0;
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean pool : pools) {
        MemoryUsage usage = pool.getUsage();
        if (pool.getType() != MemoryType.HEAP) {
            continue;
        }
        sb.append(new Formatter().format("\t\"%s\" memory used: %,d  reserved: %,d  max: %,d", pool.getName(), usage.getUsed(), usage.getCommitted(), usage.getMax()));
        totalUsed += usage.getUsed();
        totalReserved += usage.getCommitted();
        totalMax += usage.getMax();
        MemoryUsage collect = pool.getCollectionUsage();
        if (collect != null) {
            sb.append(new Formatter().format(" collectUsed: %,d", collect.getUsed()));
            if (collect.getUsed() > 0) {
                collectUsed += collect.getUsed();
            } else {
                collectUsed += usage.getUsed();
            }
        } else {
            collectUsed += usage.getUsed();
        }
        sb.append('\n');
    }
    sb.append(new Formatter().format("RuntimeTotal=%,d  RuntimeMax=%,d  RuntimeFree=%,d  TotUsed=%,d  TotReserved=%,d  TotMax=%,d  CollectUsed=%,d\n", Runtime.getRuntime().totalMemory(), Runtime.getRuntime().maxMemory(), Runtime.getRuntime().freeMemory(), totalUsed, totalReserved, totalMax, collectUsed));
    return sb.toString();
}
Also used : Formatter(java.util.Formatter) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 58 with MemoryUsage

use of java.lang.management.MemoryUsage in project geode by apache.

the class VMStats50 method refreshMemoryPools.

private void refreshMemoryPools() {
    boolean reInitPools = false;
    Iterator<Map.Entry<MemoryPoolMXBean, Statistics>> it = mpMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<MemoryPoolMXBean, Statistics> me = it.next();
        MemoryPoolMXBean mp = me.getKey();
        Statistics s = me.getValue();
        if (!mp.isValid()) {
            s.close();
            it.remove();
            reInitPools = true;
        } else {
            MemoryUsage mu = null;
            try {
                mu = mp.getUsage();
            } catch (IllegalArgumentException ex) {
                // to workaround JRockit bug 36348 just ignore this and try the next pool
                continue;
            } catch (InternalError ie) {
                // Somebody saw an InternalError once but I have no idea how to reproduce it. Was this a
                // race between
                // mp.isValid() and mp.getUsage()? Perhaps.
                s.close();
                it.remove();
                reInitPools = true;
                logger.warn("Accessing MemoryPool '{}' threw an Internal Error: {}", mp.getName(), ie.getMessage());
                continue;
            }
            s.setLong(mp_l_initMemoryId, mu.getInit());
            s.setLong(mp_l_usedMemoryId, mu.getUsed());
            s.setLong(mp_l_committedMemoryId, mu.getCommitted());
            s.setLong(mp_l_maxMemoryId, mu.getMax());
            if (mp.isUsageThresholdSupported()) {
                s.setLong(mp_usageThresholdId, mp.getUsageThreshold());
                s.setLong(mp_usageExceededId, mp.getUsageThresholdCount());
            }
            mu = null;
            if (!this.isCollectionUsageUnsupported(mp)) {
                try {
                    mu = mp.getCollectionUsage();
                } catch (UnsupportedOperationException ex) {
                    // JRockit throws this exception instead of returning null
                    // as the javadocs say it should. See bug 36348
                    this.setCollectionUsageUnsupported(mp);
                } catch (IllegalArgumentException ex) {
                    // Yet another JRockit bug in which its code catches an assertion
                    // about the state of their bean stat values being inconsistent.
                    // See bug 36348.
                    this.setCollectionUsageUnsupported(mp);
                }
            }
            if (mu != null) {
                // s.setLong(mp_gc_initMemoryId, mu.getInit());
                s.setLong(mp_gc_usedMemoryId, mu.getUsed());
                // s.setLong(mp_gc_maxMemoryId, mu.getMax());
                if (mp.isCollectionUsageThresholdSupported()) {
                    s.setLong(mp_collectionUsageThresholdId, mp.getCollectionUsageThreshold());
                    s.setLong(mp_collectionUsageExceededId, mp.getCollectionUsageThresholdCount());
                }
            }
        }
    }
    if (reInitPools) {
        initMemoryPools();
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) HashMap(java.util.HashMap) Map(java.util.Map) Statistics(org.apache.geode.Statistics) MemoryUsage(java.lang.management.MemoryUsage)

Example 59 with MemoryUsage

use of java.lang.management.MemoryUsage in project gerrit by GerritCodeReview.

the class ProcMetricModule method procJvmMemory.

private void procJvmMemory(MetricMaker metrics) {
    CallbackMetric0<Long> heapCommitted = metrics.newCallbackMetric("proc/jvm/memory/heap_committed", Long.class, new Description("Amount of memory guaranteed for user objects.").setGauge().setUnit(Units.BYTES));
    CallbackMetric0<Long> heapUsed = metrics.newCallbackMetric("proc/jvm/memory/heap_used", Long.class, new Description("Amount of memory holding user objects.").setGauge().setUnit(Units.BYTES));
    CallbackMetric0<Long> nonHeapCommitted = metrics.newCallbackMetric("proc/jvm/memory/non_heap_committed", Long.class, new Description("Amount of memory guaranteed for classes, etc.").setGauge().setUnit(Units.BYTES));
    CallbackMetric0<Long> nonHeapUsed = metrics.newCallbackMetric("proc/jvm/memory/non_heap_used", Long.class, new Description("Amount of memory holding classes, etc.").setGauge().setUnit(Units.BYTES));
    CallbackMetric0<Integer> objectPendingFinalizationCount = metrics.newCallbackMetric("proc/jvm/memory/object_pending_finalization_count", Integer.class, new Description("Approximate number of objects needing finalization.").setGauge().setUnit("objects"));
    MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
    metrics.newTrigger(ImmutableSet.<CallbackMetric<?>>of(heapCommitted, heapUsed, nonHeapCommitted, nonHeapUsed, objectPendingFinalizationCount), () -> {
        try {
            MemoryUsage stats = memory.getHeapMemoryUsage();
            heapCommitted.set(stats.getCommitted());
            heapUsed.set(stats.getUsed());
        } catch (IllegalArgumentException e) {
        // MXBean may throw due to a bug in Java 7; ignore.
        }
        MemoryUsage stats = memory.getNonHeapMemoryUsage();
        nonHeapCommitted.set(stats.getCommitted());
        nonHeapUsed.set(stats.getUsed());
        objectPendingFinalizationCount.set(memory.getObjectPendingFinalizationCount());
    });
}
Also used : Description(com.google.gerrit.metrics.Description) MemoryMXBean(java.lang.management.MemoryMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 60 with MemoryUsage

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

the class MemoryUsageCompositeData method badNameCompositeData.

public static void badNameCompositeData() throws Exception {
    final int K = 1024;
    final Object[] values = { new Long(5 * K), new Long(1 * K), new Long(10 * K), new Long(2 * K), "Dummy", "Dummy" };
    CompositeType muct = new CompositeType("MyMemoryUsageCompositeType", "CompositeType for MemoryUsage", badMUItemNames, badMUItemNames, memoryUsageItemTypes);
    CompositeData cd = new CompositeDataSupport(muct, badMUItemNames, values);
    try {
        MemoryUsage u = MemoryUsage.from(cd);
    } catch (IllegalArgumentException e) {
        System.out.println("Expected exception: " + e.getMessage());
        return;
    }
    throw new RuntimeException("IllegalArgumentException not thrown");
}
Also used : MemoryUsage(java.lang.management.MemoryUsage)

Aggregations

MemoryUsage (java.lang.management.MemoryUsage)75 MemoryMXBean (java.lang.management.MemoryMXBean)15 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)15 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 RuntimeMXBean (java.lang.management.RuntimeMXBean)6 HashMap (java.util.HashMap)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)5 ThreadMXBean (java.lang.management.ThreadMXBean)4 Map (java.util.Map)4 IOException (java.io.IOException)3 MemoryType (java.lang.management.MemoryType)3 ArrayList (java.util.ArrayList)3 NotNull (org.jetbrains.annotations.NotNull)3 ApplicationInfo (com.intellij.openapi.application.ApplicationInfo)2 GarbageCollectionNotificationInfo (com.sun.management.GarbageCollectionNotificationInfo)2 File (java.io.File)2 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)2 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)2 Date (java.util.Date)2 Properties (java.util.Properties)2