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

The following examples show how to use java.lang.management.MemoryPoolMXBean#getCollectionUsage() . 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: StabilityTestRunner.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private double calculateHeapMemoryAfterGCUsage() {
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();

    long used = 0, max = 0;

    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        String name = memoryPoolMXBean.getName();

        if (!name.contains("Eden")) {
            if (memoryPoolMXBean.getType().equals(HEAP)) {
                MemoryUsage memoryUsage = memoryPoolMXBean.getCollectionUsage();
                used += memoryUsage.getUsed();
                max += memoryUsage.getMax() == -1 ? 0 : memoryUsage.getMax();
            }
        }
    }

    return used / (double) max;
}
 
Example 3
Source File: GCStressMonitor.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
/**
 * We cache the {@link MemoryPoolMXBean}s representing heap memory pools, which should be created at JVM bootstrap
 * and available through the entire JVM lifetime.
 */
private void discoverMBeans() {
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        MemoryUsage collectionUsage = memoryPoolMXBean.getCollectionUsage();
        if (collectionUsage != null && !memoryPoolMXBean.getName().toLowerCase().contains("survivor")) {
            // Typically, the collection usage is nonnull for heap pools. The survivor pool may be problematic as
            // JVMs may frequently adjust its size, returning it back to the OS, so we cannot even rely on their max size.
            heapMBeans.add(memoryPoolMXBean);
            logger.debug("Registering a heap memory pool ({}) for stress monitoring", memoryPoolMXBean.getName());
        } else {
            logger.trace("Ignoring a non-heap memory pool ({}) for stress monitoring", memoryPoolMXBean.getName());
        }
    }
}
 
Example 4
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static long getBytesUsed(final MemoryPoolMXBean memoryPool,
    boolean getCurrentUsage) {
  if (getCurrentUsage) {
    return memoryPool.getUsage().getUsed();
  }
  else {
    final MemoryUsage usage = memoryPool.getCollectionUsage();
    if (usage != null) {
      return usage.getUsed();
    }
    else {
      return memoryPool.getUsage().getUsed();
    }
  }
}
 
Example 5
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 6
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static long getBytesUsed(final MemoryPoolMXBean memoryPool,
    boolean getCurrentUsage) {
  if (getCurrentUsage) {
    return memoryPool.getUsage().getUsed();
  }
  else {
    final MemoryUsage usage = memoryPool.getCollectionUsage();
    if (usage != null) {
      return usage.getUsed();
    }
    else {
      return memoryPool.getUsage().getUsed();
    }
  }
}
 
Example 7
Source File: GCStressMonitor.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
private boolean isThresholdCrossed(double percentageThreshold, boolean updateStressInfoIfCrossed) {
    // We apply the same threshold to all heap pools at the moment. We can rethink that if we find it is not the
    // right way to go.
    for (int i = 0; i < heapMBeans.size(); i++) {
        MemoryPoolMXBean heapPoolMBean = heapMBeans.get(i);
        MemoryUsage memUsageAfterLastGc = heapPoolMBean.getCollectionUsage();
        if (memUsageAfterLastGc != null) {
            long max = memUsageAfterLastGc.getMax();
            if (max > 0) {
                // The max is not always defined for memory pools, however it is normally defined for the Old Gen
                // pools, which are really the interesting ones.
                // Since we are tracking pool state after GC, falling back to committed may be useful: during
                // stress, committed will quickly grow to the max. As long as committed is less than max, the JVM
                // will maintain a gap of committed over used (as much as possible), otherwise allocations may fail.
                // However, should we choose to rely on committed, it will need to be restricted only for the
                // Old Generation heap pools, which means that if we are to rely on committed, we will be limited
                // to known pool names only. If we see a need going forward to use this fallback, we can investigate further.
                long bytesThreshold = (long) (percentageThreshold * max);
                long used = memUsageAfterLastGc.getUsed();
                if (bytesThreshold > 0 && used > bytesThreshold) {
                    if (updateStressInfoIfCrossed) {
                        latestStressDetectionInfo.setLength(0);
                        latestStressDetectionInfo.append("Heap pool \"").append(heapPoolMBean.getName())
                            .append("\" usage after the last GC has crossed the configured threshold ")
                            .append(percentageThreshold).append(": ").append(used).append("/").append(max)
                            .append("(used/max)");
                    } else if (logger.isDebugEnabled()) {
                        logger.debug("Heap {} pool usage after the last GC is over the threshold of {}: {}/{} (used/max)",
                            heapPoolMBean.getName(), percentageThreshold, used, max);
                    }
                    return true;
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Heap {} pool usage after the last GC is below the threshold of {}: {}/{} (used/max)",
                            heapPoolMBean.getName(), percentageThreshold, used, max);
                    }
                }
            }
        } else {
            logger.debug("Collection usage cannot be obtained from heap pool MBean {}", heapPoolMBean.getName());
        }
    }
    return false;
}
 
Example 8
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()));
            }
        }
    }
}