Search in sources :

Example 11 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 12 with MemoryPoolMXBean

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

the class MemoryUsageSupport method getMemoryPoolsJson.

final String getMemoryPoolsJson() {
    final StringBuilder buf = new StringBuilder();
    buf.append("[");
    long usedTotal = 0;
    long initTotal = 0;
    long committedTotal = 0;
    long maxTotal = 0;
    final List<MemoryPoolMXBean> pools = getMemoryPools();
    for (MemoryPoolMXBean pool : pools) {
        buf.append("{");
        buf.append("'name':'").append(pool.getName()).append('\'');
        buf.append(",'type':'").append(pool.getType()).append('\'');
        MemoryUsage usage = pool.getUsage();
        final long used = usage.getUsed();
        formatNumber(buf, "used", used);
        if (used > -1) {
            usedTotal += used;
        }
        final long init = usage.getInit();
        formatNumber(buf, "init", init);
        if (init > -1) {
            initTotal += init;
        }
        final long committed = usage.getCommitted();
        formatNumber(buf, "committed", committed);
        committedTotal += committed;
        final long max = usage.getMax();
        formatNumber(buf, "max", usage.getMax());
        final long score;
        if (max == -1 || used == -1) {
            score = 100;
        } else {
            maxTotal += max;
            score = 100L * used / max;
        }
        buf.append(",'score':'").append(score).append("%'");
        buf.append("},");
    }
    // totalisation
    buf.append("{");
    buf.append("'name':'Total','type':'TOTAL'");
    formatNumber(buf, "used", usedTotal);
    formatNumber(buf, "init", initTotal);
    formatNumber(buf, "committed", committedTotal);
    formatNumber(buf, "max", maxTotal);
    final long score = 100L * usedTotal / maxTotal;
    buf.append(",'score':'").append(score).append("%'");
    buf.append("}");
    buf.append("]");
    return buf.toString();
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 13 with MemoryPoolMXBean

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

the class MemoryUsageSupport method printMemoryPools.

final void printMemoryPools(final PrintHelper pw) {
    final List<MemoryPoolMXBean> pools = getMemoryPools();
    for (MemoryPoolMXBean pool : pools) {
        final String title = String.format("%s (%s, %s)", pool.getName(), pool.getType(), (pool.isValid() ? "valid" : "invalid"));
        pw.title(title, 1);
        pw.keyVal("Memory Managers", Arrays.asList(pool.getMemoryManagerNames()));
        pw.keyVal("Peak Usage", pool.getPeakUsage());
        pw.keyVal("Usage", pool.getUsage());
        if (pool.isUsageThresholdSupported()) {
            pw.keyVal("Usage Threshold", String.format("%d, %s, #exceeded=%d", pool.getUsageThreshold(), pool.isUsageThresholdExceeded() ? "exceeded" : "not exceeded", pool.getUsageThresholdCount()));
        } else {
            pw.val("Usage Threshold: not supported");
        }
        pw.keyVal("Collection Usage", pool.getCollectionUsage());
        if (pool.isCollectionUsageThresholdSupported()) {
            pw.keyVal("Collection Usage Threshold", String.format("%d, %s, #exceeded=%d", pool.getCollectionUsageThreshold(), pool.isCollectionUsageThresholdExceeded() ? "exceeded" : "not exceeded", pool.getCollectionUsageThresholdCount()));
        } else {
            pw.val("Collection Usage Threshold: not supported");
        }
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 14 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project leopard by tanhaichao.

the class JvmManagement method printMemoryPool.

public static void printMemoryPool() {
    // 获取多个内存池的使用情况
    System.out.println("=======================MemoryPoolMXBean============================ ");
    List<MemoryPoolMXBean> mpmList = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean mpm : mpmList) {
        System.out.println("getUsage " + mpm.getUsage());
        System.out.println("getMemoryManagerNames " + StringUtils.join(mpm.getMemoryManagerNames(), ","));
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 15 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project qpid-broker-j by apache.

the class BrokerAttributeInjector method getInjectedAttributes.

@Override
public Collection<ConfiguredObjectInjectedAttribute<?, ?>> getInjectedAttributes() {
    List<ConfiguredObjectInjectedAttribute<?, ?>> attributes = new ArrayList<>();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        String poolName = memoryPoolMXBean.getName().replace(" ", "");
        String attributeName = "jvmMemoryMaximum" + poolName;
        try {
            Method getMemoryPoolMaximum = BrokerAttributeInjector.class.getDeclaredMethod("getMemoryPoolMaximum", Broker.class, MemoryPoolMXBean.class);
            final ConfiguredObjectInjectedAttribute<?, ?> injectedStatistic = new ConfiguredDerivedInjectedAttribute<>(attributeName, getMemoryPoolMaximum, new Object[] { memoryPoolMXBean }, false, false, "", false, "", "Maximum size of memory pool " + memoryPoolMXBean.getName(), _typeValidator);
            attributes.add(injectedStatistic);
        } catch (NoSuchMethodException e) {
            LOGGER.warn("Failed to inject attribute '{}'", attributeName, e);
        }
    }
    return attributes;
}
Also used : ArrayList(java.util.ArrayList) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Method(java.lang.reflect.Method)

Aggregations

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