Search in sources :

Example 51 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project gradle by gradle.

the class GarbageCollectionCheck method run.

@Override
public void run() {
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        String poolName = memoryPoolMXBean.getName();
        if (memoryPoolMXBean.getType() == MemoryType.HEAP && poolName.equals(heapMemoryPool)) {
            GarbageCollectionEvent latest = heapEvents.latest();
            long currentCount = garbageCollectorMXBean.getCollectionCount();
            // There has been a GC event
            if (latest == null || latest.getCount() != currentCount) {
                heapEvents.slideAndInsert(new GarbageCollectionEvent(clock.getCurrentTime(), memoryPoolMXBean.getCollectionUsage(), currentCount));
            }
        }
        if (memoryPoolMXBean.getType() == MemoryType.NON_HEAP && poolName.equals(nonHeapMemoryPool)) {
            nonHeapEvents.slideAndInsert(new GarbageCollectionEvent(clock.getCurrentTime(), memoryPoolMXBean.getUsage(), -1));
        }
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 52 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project metrics by dropwizard.

the class MemoryUsageGaugeSet method getMetrics.

@Override
public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<>();
    gauges.put("total.init", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getInit() + mxBean.getNonHeapMemoryUsage().getInit());
    gauges.put("total.used", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getUsed() + mxBean.getNonHeapMemoryUsage().getUsed());
    gauges.put("total.max", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getMax() + mxBean.getNonHeapMemoryUsage().getMax());
    gauges.put("total.committed", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getCommitted() + mxBean.getNonHeapMemoryUsage().getCommitted());
    gauges.put("heap.init", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getInit());
    gauges.put("heap.used", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getUsed());
    gauges.put("heap.max", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getMax());
    gauges.put("heap.committed", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getCommitted());
    gauges.put("heap.usage", new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            final MemoryUsage usage = mxBean.getHeapMemoryUsage();
            return Ratio.of(usage.getUsed(), usage.getMax());
        }
    });
    gauges.put("non-heap.init", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getInit());
    gauges.put("non-heap.used", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getUsed());
    gauges.put("non-heap.max", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getMax());
    gauges.put("non-heap.committed", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getCommitted());
    gauges.put("non-heap.usage", new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            final MemoryUsage usage = mxBean.getNonHeapMemoryUsage();
            return Ratio.of(usage.getUsed(), usage.getMax());
        }
    });
    for (final MemoryPoolMXBean pool : memoryPools) {
        final String poolName = name("pools", WHITESPACE.matcher(pool.getName()).replaceAll("-"));
        gauges.put(name(poolName, "usage"), new RatioGauge() {

            @Override
            protected Ratio getRatio() {
                MemoryUsage usage = pool.getUsage();
                return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax());
            }
        });
        gauges.put(name(poolName, "max"), (Gauge<Long>) () -> pool.getUsage().getMax());
        gauges.put(name(poolName, "used"), (Gauge<Long>) () -> pool.getUsage().getUsed());
        gauges.put(name(poolName, "committed"), (Gauge<Long>) () -> pool.getUsage().getCommitted());
        // Only register GC usage metrics if the memory pool supports usage statistics.
        if (pool.getCollectionUsage() != null) {
            gauges.put(name(poolName, "used-after-gc"), (Gauge<Long>) () -> pool.getCollectionUsage().getUsed());
        }
        gauges.put(name(poolName, "init"), (Gauge<Long>) () -> pool.getUsage().getInit());
    }
    return Collections.unmodifiableMap(gauges);
}
Also used : HashMap(java.util.HashMap) RatioGauge(com.codahale.metrics.RatioGauge) Metric(com.codahale.metrics.Metric) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 53 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 54 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project checker-framework by typetools.

the class SourceChecker method printStats.

/**
 * Print resource usage statistics.
 */
protected void printStats() {
    List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPool : memoryPools) {
        System.out.println("Memory pool " + memoryPool.getName() + " statistics");
        System.out.println("  Pool type: " + memoryPool.getType());
        System.out.println("  Peak usage: " + memoryPool.getPeakUsage());
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 55 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project oap by oaplatform.

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)

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