Search in sources :

Example 91 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Mindroid.java by Himmele.

the class ConsoleService method getMemoryUsage.

private static String getMemoryUsage() {
    StringBuilder builder = new StringBuilder();
    MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage totalHeapMemoryUsage = memoryBean.getHeapMemoryUsage();
    MemoryUsage totalNonHeapMemoryUsage = memoryBean.getNonHeapMemoryUsage();
    builder.append("Summary:").append(System.lineSeparator());
    builder.append("Heap memory:  ");
    addMemoryUsageSummary(builder, totalHeapMemoryUsage);
    builder.append(System.lineSeparator());
    builder.append("Non-heap memory: ");
    addMemoryUsageSummary(builder, totalNonHeapMemoryUsage);
    builder.append(System.lineSeparator()).append(System.lineSeparator());
    List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
    List<MemoryPoolMXBean> heapMemoryPools = new ArrayList<>();
    List<MemoryPoolMXBean> nonHeapMemoryPools = new ArrayList<>();
    for (MemoryPoolMXBean pool : memoryPools) {
        if (pool.isValid()) {
            if (pool.getType() == MemoryType.HEAP) {
                heapMemoryPools.add(pool);
            } else {
                nonHeapMemoryPools.add(pool);
            }
        }
    }
    builder.append("Heap memory:").append(System.lineSeparator());
    builder.append("Total heap memory:");
    addMemoryUsage(builder, totalHeapMemoryUsage);
    builder.append(System.lineSeparator());
    for (MemoryPoolMXBean pool : heapMemoryPools) {
        addMemoryPool(builder, pool);
        builder.append(System.lineSeparator());
    }
    builder.append(System.lineSeparator()).append("Non-heap memory:").append(System.lineSeparator());
    builder.append("Total non-heap memory:");
    addMemoryUsage(builder, totalNonHeapMemoryUsage);
    builder.append(System.lineSeparator());
    for (MemoryPoolMXBean pool : nonHeapMemoryPools) {
        addMemoryPool(builder, pool);
        builder.append(System.lineSeparator());
    }
    List<BufferPoolMXBean> buffers = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    builder.append(System.lineSeparator()).append("Buffers:").append(System.lineSeparator());
    for (BufferPoolMXBean buffer : buffers) {
        builder.append(buffer.getName()).append(":");
        builder.append(" count ").append(buffer.getCount());
        builder.append(" memory ").append(buffer.getMemoryUsed()).append("B");
        builder.append(System.lineSeparator());
    }
    return builder.toString();
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) BufferPoolMXBean(java.lang.management.BufferPoolMXBean) ArrayList(java.util.ArrayList) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 92 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project siddhi by wso2.

the class ObjectSizeCalculator method getEffectiveMemoryLayoutSpecification.

@VisibleForTesting
static MemoryLayoutSpecification getEffectiveMemoryLayoutSpecification() {
    final String vmName = System.getProperty("java.vm.name");
    if (vmName == null || !(vmName.startsWith("Java HotSpot(TM) ") || vmName.startsWith("OpenJDK") || vmName.startsWith("TwitterJDK"))) {
        throw new UnsupportedOperationException("ObjectSizeCalculator only supported on HotSpot VM");
    }
    final String dataModel = System.getProperty("sun.arch.data.model");
    if ("32".equals(dataModel)) {
        // Running with 32-bit data model.
        return new MemoryLayoutSpecification() {

            @Override
            public int getArrayHeaderSize() {
                return 12;
            }

            @Override
            public int getObjectHeaderSize() {
                return 8;
            }

            @Override
            public int getObjectPadding() {
                return 8;
            }

            @Override
            public int getReferenceSize() {
                return 4;
            }

            @Override
            public int getSuperclassFieldPadding() {
                return 4;
            }
        };
    } else if (!"64".equals(dataModel)) {
        throw new UnsupportedOperationException("Unrecognized value '" + dataModel + "' of sun.arch.data.model system property");
    }
    final String strVmVersion = System.getProperty("java.vm.version");
    final int vmVersion = Integer.parseInt(strVmVersion.substring(0, strVmVersion.indexOf('.')));
    if (vmVersion >= 17) {
        long maxMemory = 0;
        for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
            maxMemory += mp.getUsage().getMax();
        }
        if (maxMemory < 30L * 1024 * 1024 * 1024) {
            // for all memory pools (yes, including code cache).
            return new MemoryLayoutSpecification() {

                @Override
                public int getArrayHeaderSize() {
                    return 16;
                }

                @Override
                public int getObjectHeaderSize() {
                    return 12;
                }

                @Override
                public int getObjectPadding() {
                    return 8;
                }

                @Override
                public int getReferenceSize() {
                    return 4;
                }

                @Override
                public int getSuperclassFieldPadding() {
                    return 4;
                }
            };
        }
    }
    // In other cases, it's a 64-bit uncompressed OOPs object model
    return new MemoryLayoutSpecification() {

        @Override
        public int getArrayHeaderSize() {
            return 24;
        }

        @Override
        public int getObjectHeaderSize() {
            return 16;
        }

        @Override
        public int getObjectPadding() {
            return 8;
        }

        @Override
        public int getReferenceSize() {
            return 8;
        }

        @Override
        public int getSuperclassFieldPadding() {
            return 8;
        }
    };
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 93 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean 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 94 with MemoryPoolMXBean

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

the class MemoryStats method getHeapPoolNames.

/**
 * @return A list of the HEAP memory pools in the VM, that is the pools used for dynamic
 * allocations.  Non-HEAP pools (code cache, permanent gen, etc) are not included
 *
 * For HotSpot default settings, the memory pools are:
 *    Eden (young gen) -- new allocs go here.  Managed by Copy Collector.
 *    Survivor
 *    Tenured -- long-lived objects.  Managed by MarkSweepCompact Collector
 */
public static String[] getHeapPoolNames() {
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    ArrayList<String> array = new ArrayList<String>(pools.size());
    for (MemoryPoolMXBean pool : pools) {
        // for bug 16398, include non-heap pools (so we get permgen info)
        // if (pool.getType() != MemoryType.HEAP) {
        // continue;
        // }
        array.add(pool.getName());
    }
    String[] toRet = new String[array.size()];
    return array.toArray(toRet);
}
Also used : ArrayList(java.util.ArrayList) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 95 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean 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)

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