Java Code Examples for java.lang.management.MemoryPoolMXBean#getPeakUsage()

The following examples show how to use java.lang.management.MemoryPoolMXBean#getPeakUsage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Memory.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a summary information about the memory pools.
 */
public static String poolSummaries() {
    // Why ? list-archive?4273859
    // How ? http://stackoverflow.com/questions/697336/how-do-i-programmatically-find-out-my-permgen-space-usage
    //       http://stackoverflow.com/questions/8356416/xxmaxpermsize-with-or-without-xxpermsize
    StringBuilder sb = new StringBuilder();
    for (MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
        String name = item.getName();
        MemoryType type = item.getType();
        MemoryUsage usage = item.getUsage();
        MemoryUsage peak = item.getPeakUsage();
        MemoryUsage collections = item.getCollectionUsage();
        sb.append("Memory pool name: " + name
                  + ", type: " + type
                  + ", usage: " + usage
                  + ", peak: " + peak
                  + ", collections: " + collections
                  + "\n");
    }
    return sb.toString();
}
 
Example 2
Source File: MemoryPoolPeakUsageTask.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public DiagnosticsDumpElement captureDump(final boolean verbose) {
    final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();

    final List<String> details = new ArrayList<>();
    for (final MemoryPoolMXBean poolBean : pools) {
        final String poolName = poolBean.getName();
        final MemoryUsage usage = poolBean.getPeakUsage();
        final long maxUsed = usage.getUsed();
        final long maxAvailable = usage.getMax();

        final String maxUsageDescription;
        if (maxAvailable > 0) {
            final double percentage = maxUsed * 100D / maxAvailable;
            maxUsageDescription = String.format("%1$,d bytes, %2$.2f%%", maxUsed, percentage);
        } else {
            maxUsageDescription = String.format("%1$,d bytes", maxUsed);
        }

        details.add(poolName + ": " + maxUsageDescription);
    }

    return new StandardDiagnosticsDumpElement("Memory Pool Peak Usage", details);
}
 
Example 3
Source File: JVMMetrics.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
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(ByteAmount.fromBytes(peakUsage.getUsed()).asMegabytes());
      jvmPeakUsagePerMemoryPool.safeScope(normalizedKeyName + "-committed")
          .setValue(ByteAmount.fromBytes(peakUsage.getCommitted()).asMegabytes());
      jvmPeakUsagePerMemoryPool.safeScope(normalizedKeyName + "-max")
          .setValue(ByteAmount.fromBytes(peakUsage.getMax()).asMegabytes());
    }

    MemoryUsage collectionUsage = memoryPoolMXBean.getCollectionUsage();
    if (collectionUsage != null) {
      jvmCollectionUsagePerMemoryPool.safeScope(normalizedKeyName + "-used")
          .setValue(ByteAmount.fromBytes(collectionUsage.getUsed()).asMegabytes());
      jvmCollectionUsagePerMemoryPool.safeScope(normalizedKeyName + "-committed")
          .setValue(ByteAmount.fromBytes(collectionUsage.getCommitted()).asMegabytes());
      jvmCollectionUsagePerMemoryPool.safeScope(normalizedKeyName + "-max")
          .setValue(ByteAmount.fromBytes(collectionUsage.getMax()).asMegabytes());
    }

    MemoryUsage estimatedUsage = memoryPoolMXBean.getUsage();
    if (estimatedUsage != null) {
      jvmEstimatedUsagePerMemoryPool.safeScope(normalizedKeyName + "-used")
          .setValue(ByteAmount.fromBytes(estimatedUsage.getUsed()).asMegabytes());
      jvmEstimatedUsagePerMemoryPool.safeScope(normalizedKeyName + "-committed")
          .setValue(ByteAmount.fromBytes(estimatedUsage.getCommitted()).asMegabytes());
      jvmEstimatedUsagePerMemoryPool.safeScope(normalizedKeyName + "-max")
          .setValue(ByteAmount.fromBytes(estimatedUsage.getMax()).asMegabytes());
    }
  }
}
 
Example 4
Source File: MonteCarloBlackScholesModelAsianOptionSensitivitiesTest.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
private long memoryUsagePeak() {
	long sumOfPeak = 0;
	for (final MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
		final MemoryUsage peak = pool.getPeakUsage();
		sumOfPeak += peak.getUsed();
	}
	return sumOfPeak;
}
 
Example 5
Source File: MonteCarloBlackScholesModelEuropeanOptionSensitivitiesTest.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
private long memoryUsagePeak() {
	long sumOfPeak = 0;
	for (final MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
		final MemoryUsage peak = pool.getPeakUsage();
		sumOfPeak += peak.getUsed();
	}
	return sumOfPeak;
}
 
Example 6
Source File: MonteCarloBlackScholesModelAsianOptionSensitivitiesTest.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
private long memoryUsagePeak() {
	long sumOfPeak = 0;
	for (final MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
		final MemoryUsage peak = pool.getPeakUsage();
		sumOfPeak += peak.getUsed();
	}
	return sumOfPeak;
}
 
Example 7
Source File: MonteCarloBlackScholesModelEuropeanOptionSensitivitiesTest.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
private long memoryUsagePeak() {
	long sumOfPeak = 0;
	for (final MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
		final MemoryUsage peak = pool.getPeakUsage();
		sumOfPeak += peak.getUsed();
	}
	return sumOfPeak;
}
 
Example 8
Source File: CPlanner.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/** Logs memory usage of the JVM */
private void logMemoryUsage() {
    try {
        String memoryUsage = new String();
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
        double totalUsed = 0; // in bytes
        double totalReserved = 0; // in bytes
        double divisor = 1024 * 1024; // display stats in MB
        for (MemoryPoolMXBean pool : pools) {
            MemoryUsage peak = pool.getPeakUsage();
            totalUsed += peak.getUsed();
            totalReserved += peak.getCommitted();
            memoryUsage +=
                    String.format(
                            "Peak %s memory used    : %.3f MB%n",
                            pool.getName(), peak.getUsed() / divisor);
            memoryUsage +=
                    String.format(
                            "Peak %s memory reserved: %.3f MB%n",
                            pool.getName(), peak.getCommitted() / divisor);
        }

        // we print the result in the console
        mLogger.log(
                "JVM Memory Usage Breakdown \n" + memoryUsage.toString(),
                LogManager.INFO_MESSAGE_LEVEL);
        mLogger.log(
                String.format("Total Peak memory used      : %.3f MB", totalUsed / divisor),
                LogManager.INFO_MESSAGE_LEVEL);
        mLogger.log(
                String.format("Total Peak memory reserved  : %.3f MB", totalReserved / divisor),
                LogManager.INFO_MESSAGE_LEVEL);
    } catch (Throwable t) {
        // not fatal
        mLogger.log(
                "Error while logging peak memory usage " + t.getMessage(),
                LogManager.ERROR_MESSAGE_LEVEL);
    }
}
 
Example 9
Source File: SmallRyeMetricsRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void memoryPoolMetrics(MetricRegistry registry) {
    // MemoryPoolMXBean doesn't work in native mode
    if (!ImageInfo.inImageCode()) {
        List<MemoryPoolMXBean> mps = ManagementFactory.getMemoryPoolMXBeans();
        Metadata usageMetadata = Metadata.builder()
                .withName("memoryPool.usage")
                .withType(MetricType.GAUGE)
                .withDisplayName("Current usage of the memory pool denoted by the 'name' tag")
                .withDescription("Current usage of the memory pool denoted by the 'name' tag")
                .withUnit(MetricUnits.BYTES)
                .build();
        Metadata maxMetadata = Metadata.builder()
                .withName("memoryPool.usage.max")
                .withType(MetricType.GAUGE)
                .withDisplayName("Peak usage of the memory pool denoted by the 'name' tag")
                .withDescription("Peak usage of the memory pool denoted by the 'name' tag")
                .withUnit(MetricUnits.BYTES)
                .build();
        for (MemoryPoolMXBean mp : mps) {
            if (mp.getCollectionUsage() != null && mp.getPeakUsage() != null) {
                // this will be the case for the heap memory pools
                registry.register(usageMetadata, new Gauge() {
                    @Override
                    public Number getValue() {
                        return mp.getCollectionUsage().getUsed();
                    }
                },
                        new Tag("name", mp.getName()));

                registry.register(maxMetadata, new Gauge() {
                    @Override
                    public Number getValue() {
                        return mp.getPeakUsage().getUsed();
                    }
                },
                        new Tag("name", mp.getName()));
            } else if (mp.getUsage() != null && mp.getPeakUsage() != null) {
                // this will be the case for the non-heap memory pools
                registry.register(usageMetadata, new Gauge() {
                    @Override
                    public Number getValue() {
                        return mp.getUsage().getUsed();
                    }
                },
                        new Tag("name", mp.getName()));

                registry.register(maxMetadata, new Gauge() {
                    @Override
                    public Number getValue() {
                        return mp.getPeakUsage().getUsed();
                    }
                },
                        new Tag("name", mp.getName()));
            }
        }
    }
}
 
Example 10
Source File: JvmInfo.java    From elasticsearch-helper with Apache License 2.0 4 votes vote down vote up
public static Stats readStats() {
    Stats stats = new Stats();
    stats.timestamp = System.currentTimeMillis();
    stats.uptime = runtimeMXBean.getUptime();
    stats.mem = new Mem();

    MemoryUsage memUsage = memoryMXBean.getHeapMemoryUsage();
    stats.mem.heapInit = memUsage.getInit() < 0 ? 0 : memUsage.getInit();
    stats.mem.heapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    stats.mem.heapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    stats.mem.heapMax = memUsage.getMax() < 0 ? 0 : memUsage.getMax();

    memUsage = memoryMXBean.getNonHeapMemoryUsage();
    stats.mem.nonHeapInit = memUsage.getInit() < 0 ? 0 : memUsage.getInit();
    stats.mem.nonHeapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    stats.mem.nonHeapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    stats.mem.nonHeapMax = memUsage.getMax() < 0 ? 0 : memUsage.getMax();

    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    List<MemoryPool> pools = new ArrayList<>();
    for (int i = 0; i < memoryPoolMXBeans.size(); i++) {
        try {
            MemoryPoolMXBean memoryPoolMXBean = memoryPoolMXBeans.get(i);
            MemoryUsage usage = memoryPoolMXBean.getUsage();
            MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
            String name = 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 (OutOfMemoryError err) {
            throw err; // rethrow
        } catch (Throwable ex) {
            /* ignore some JVMs might barf here with:
             * java.lang.InternalError: Memory Pool not found
             * we just omit the pool in that case!*/
        }
    }
    stats.mem.pools = pools.toArray(new MemoryPool[pools.size()]);

    stats.threads = new Threads();
    stats.threads.count = threadMXBean.getThreadCount();
    stats.threads.peakCount = threadMXBean.getPeakThreadCount();



    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    stats.gc = new GarbageCollectors();
    stats.gc.collectors = new GarbageCollector[gcMxBeans.size()];
    for (int i = 0; i < stats.gc.collectors.length; i++) {
        GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
        stats.gc.collectors[i] = new GarbageCollector();
        stats.gc.collectors[i].name = getByGcName(gcMxBean.getName(), gcMxBean.getName());
        stats.gc.collectors[i].collectionCount = gcMxBean.getCollectionCount();
        stats.gc.collectors[i].collectionTime = gcMxBean.getCollectionTime();
    }

    try {
        List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
        stats.bufferPools = new ArrayList<>(bufferPools.size());
        for (BufferPoolMXBean bufferPool : bufferPools) {
            stats.bufferPools.add(new BufferPool(bufferPool.getName(), bufferPool.getCount(), bufferPool.getTotalCapacity(), bufferPool.getMemoryUsed()));
        }
    } catch (Throwable t) {
        // buffer pools are not available
    }

    stats.classes = new Classes();
    stats.classes.loadedClassCount = classLoadingMXBean.getLoadedClassCount();
    stats.classes.totalLoadedClassCount = classLoadingMXBean.getTotalLoadedClassCount();
    stats.classes.unloadedClassCount = classLoadingMXBean.getUnloadedClassCount();

    return stats;
}
 
Example 11
Source File: JvmStats.java    From crate with Apache License 2.0 4 votes vote down vote up
public static JvmStats jvmStats() {
    MemoryUsage memUsage = MEMORY_MXBEAN.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 = MEMORY_MXBEAN.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 (final Exception ignored) {

        }
    }
    Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, Collections.unmodifiableList(pools));
    Threads threads = new Threads(THREAD_MXBEAN.getThreadCount(), THREAD_MXBEAN.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(CLASS_LOADING_MXBEAN.getLoadedClassCount(), CLASS_LOADING_MXBEAN.getTotalLoadedClassCount(),
            CLASS_LOADING_MXBEAN.getUnloadedClassCount());

    return new JvmStats(System.currentTimeMillis(), RUNTIME_MXBEAN.getUptime(), mem, threads,
            garbageCollectors, bufferPoolsList, classes);
}