java.lang.management.MemoryPoolMXBean Java Examples

The following examples show how to use java.lang.management.MemoryPoolMXBean. 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: JVMMonitor.java    From fqueue with Apache License 2.0 6 votes vote down vote up
public static Map<String, MemoryUsage> getMemoryPoolCollectionUsage() {
    Map<String, MemoryUsage> gcMemory = new HashMap<String, MemoryUsage>();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        String name = memoryPoolMXBean.getName();
        if (edenSpace.contains(name)) {
            gcMemory.put("eden", memoryPoolMXBean.getCollectionUsage());
        } else if (survivorSpace.contains(name)) {
            gcMemory.put("survivor", memoryPoolMXBean.getCollectionUsage());
        } else if (oldSpace.contains(name)) {
            gcMemory.put("old", memoryPoolMXBean.getCollectionUsage());
        } else if (permSpace.contains(name)) {
            gcMemory.put("perm", memoryPoolMXBean.getCollectionUsage());
        } else if (codeCacheSpace.contains(name)) {
            gcMemory.put("codeCache", memoryPoolMXBean.getCollectionUsage());
        }

    }
    return gcMemory;
}
 
Example #2
Source File: MemoryPoolMXBeanMetricFamilyCollector.java    From cassandra-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public Stream<MetricFamily> collect() {
    final Stream.Builder<NumericMetric> initialBytesMetrics = Stream.builder();
    final Stream.Builder<NumericMetric> usedBytesMetrics = Stream.builder();
    final Stream.Builder<NumericMetric> committedBytesMetrics = Stream.builder();
    final Stream.Builder<NumericMetric> maximumBytesMetrics = Stream.builder();

    for (final Map.Entry<Labels, MemoryPoolMXBean> entry : labeledMemoryPoolMXBeans.entrySet()) {
        final Labels labels = entry.getKey();
        final MemoryPoolMXBean memoryPoolMXBean = entry.getValue();

        final MemoryUsage usage = memoryPoolMXBean.getUsage();

        initialBytesMetrics.add(new NumericMetric(labels, neg1ToNaN(usage.getInit())));
        usedBytesMetrics.add(new NumericMetric(labels, usage.getUsed()));
        committedBytesMetrics.add(new NumericMetric(labels, usage.getCommitted()));
        maximumBytesMetrics.add(new NumericMetric(labels, neg1ToNaN(usage.getMax())));
    }

    return Stream.of(
            new GaugeMetricFamily("cassandra_jvm_memory_pool_initial_bytes", "Initial size of the memory pool.", initialBytesMetrics.build()),
            new GaugeMetricFamily("cassandra_jvm_memory_pool_used_bytes", "Current memory pool usage.", usedBytesMetrics.build()),
            new GaugeMetricFamily("cassandra_jvm_memory_pool_committed_bytes", null, committedBytesMetrics.build()),
            new GaugeMetricFamily("cassandra_jvm_memory_pool_maximum_bytes", "Maximum size of the memory pool.", maximumBytesMetrics.build())
    );
}
 
Example #3
Source File: GarbageProducer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private List<Object> eatMetaspace(float targetUsage) {
    List<Object> list = new ArrayList<>();
    MemoryPoolMXBean metaspacePool = getMatchedMemoryPool(".*Metaspace.*");
    float currentUsage;
    GeneratedClassProducer gp = new GeneratedClassProducer();
    do {
        try {
            list.add(gp.create(0));
        } catch (OutOfMemoryError oome) {
            list = null;
            throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace.");
        }
        MemoryUsage memoryUsage = metaspacePool.getUsage();
        currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax());
    } while (currentUsage < targetUsage);
    return list;
}
 
Example #4
Source File: JVMMemory.java    From mpush with Apache License 2.0 6 votes vote down vote up
public JVMMemory() {
    memoryMXBean = ManagementFactory.getMemoryMXBean();
    List<MemoryPoolMXBean> list = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean item : list) {
        String name = item.getName();
        if (permGenName.contains(name)) {
            permGenMxBean = item;
        } else if (oldGenName.contains(name)) {
            oldGenMxBean = item;
        } else if (edenSpaceName.contains(name)) {
            edenSpaceMxBean = item;
        } else if (psSurvivorName.contains(name)) {
            pSSurvivorSpaceMxBean = item;
        }
    }
}
 
Example #5
Source File: JVMMonitor.java    From fqueue with Apache License 2.0 6 votes vote down vote up
public static Map<String, MemoryUsage> getMemoryPoolPeakUsage() {
    Map<String, MemoryUsage> gcMemory = new HashMap<String, MemoryUsage>();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        String name = memoryPoolMXBean.getName();
        if (edenSpace.contains(name)) {
            gcMemory.put("eden", memoryPoolMXBean.getPeakUsage());
        } else if (survivorSpace.contains(name)) {
            gcMemory.put("survivor", memoryPoolMXBean.getPeakUsage());
        } else if (oldSpace.contains(name)) {
            gcMemory.put("old", memoryPoolMXBean.getPeakUsage());
        } else if (permSpace.contains(name)) {
            gcMemory.put("perm", memoryPoolMXBean.getPeakUsage());
        } else if (codeCacheSpace.contains(name)) {
            gcMemory.put("codeCache", memoryPoolMXBean.getPeakUsage());
        }

    }
    return gcMemory;
}
 
Example #6
Source File: JvmMemMgrPoolRelTableMetaImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a map pool-name => pool-index from the SnmpTableHandler
 * of the JvmMemPoolTable.
 **/
private static Map<String, SnmpOid> buildPoolIndexMap(SnmpTableHandler handler) {
    // optimization...
    if (handler instanceof SnmpCachedData)
        return buildPoolIndexMap((SnmpCachedData)handler);

    // not optimizable... too bad.
    final Map<String, SnmpOid> m = new HashMap<>();
    SnmpOid index=null;
    while ((index = handler.getNext(index))!=null) {
        final MemoryPoolMXBean mpm =
            (MemoryPoolMXBean)handler.getData(index);
        if (mpm == null) continue;
        final String name = mpm.getName();
        if (name == null) continue;
        m.put(name,index);
    }
    return m;
}
 
Example #7
Source File: JvmMemMgrPoolRelTableMetaImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a map pool-name => pool-index from the SnmpTableHandler
 * of the JvmMemPoolTable.
 * Optimized algorithm.
 **/
private static Map<String, SnmpOid> buildPoolIndexMap(SnmpCachedData cached) {
    if (cached == null) return Collections.emptyMap();
    final SnmpOid[] indexes = cached.indexes;
    final Object[]  datas   = cached.datas;
    final int len = indexes.length;
    final Map<String, SnmpOid> m = new HashMap<>(len);
    for (int i=0; i<len; i++) {
        final SnmpOid index = indexes[i];
        if (index == null) continue;
        final MemoryPoolMXBean mpm =
            (MemoryPoolMXBean)datas[i];
        if (mpm == null) continue;
        final String name = mpm.getName();
        if (name == null) continue;
        m.put(name,index);
    }
    return m;
}
 
Example #8
Source File: JvmMemMgrPoolRelTableMetaImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a map pool-name => pool-index from the SnmpTableHandler
 * of the JvmMemPoolTable.
 **/
private static Map<String, SnmpOid> buildPoolIndexMap(SnmpTableHandler handler) {
    // optimization...
    if (handler instanceof SnmpCachedData)
        return buildPoolIndexMap((SnmpCachedData)handler);

    // not optimizable... too bad.
    final Map<String, SnmpOid> m = new HashMap<>();
    SnmpOid index=null;
    while ((index = handler.getNext(index))!=null) {
        final MemoryPoolMXBean mpm =
            (MemoryPoolMXBean)handler.getData(index);
        if (mpm == null) continue;
        final String name = mpm.getName();
        if (name == null) continue;
        m.put(name,index);
    }
    return m;
}
 
Example #9
Source File: MemoryPoolProxy.java    From jmxmon with Apache License 2.0 6 votes vote down vote up
public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException {
    this.client = client;
    this.objName = objName;
    this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class);
    this.poolName = this.pool.getName();
    this.gcMBeans = new HashMap<ObjectName,Long>();
    this.lastGcInfo = null;

    String[] mgrNames = pool.getMemoryManagerNames();
    for (String name : mgrNames) {
        try {
            ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
                                                  ",name=" + name);
            if (client.isRegistered(mbeanName)) {
                gcMBeans.put(mbeanName, new Long(0));
            }
        } catch (Exception e) {
            throw new IllegalStateException(e);
        } 
   
    } 
}
 
Example #10
Source File: MemoryMonitor.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
public MemoryMonitor() {
    LOG.info("initializing");
    this.springContextId = "Unknown";
    ManagementFactory.getThreadMXBean().setThreadContentionMonitoringEnabled(true);
    ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true);
    lowMemoryListener = new NotificationListener() {
        public void handleNotification(Notification n, Object hb) {
            if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
                Map<String, String> memoryUsageStatistics = new HashMap<String, String>();
                memoryUsageStatistics.put("MemoryMXBean: " + MemoryType.HEAP, ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString());
                memoryUsageStatistics.put("MemoryMXBean:" + MemoryType.NON_HEAP, ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().toString());
                for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
                    memoryUsageStatistics.put("MemoryPoolMXBean: " + pool.getType(), pool.getUsage().toString());
                }
                for (Listener listener : listeners) {
                    listener.memoryUsageLow(springContextId, memoryUsageStatistics, Arrays.toString(ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads()));
                }
            }
        }
    };
    ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(lowMemoryListener, null, null);
}
 
Example #11
Source File: JmxMemoryPoolManager.java    From vjtools with Apache License 2.0 6 votes vote down vote up
public JmxMemoryPoolManager(MBeanServerConnection connection) throws IOException {
	List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getPlatformMXBeans(connection,
			MemoryPoolMXBean.class);
	for (MemoryPoolMXBean memoryPool : memoryPoolMXBeans) {
		String name = memoryPool.getName().trim();
		String lowerCaseName = name.toLowerCase();
		if (lowerCaseName.contains(SURVIVOR)) {
			survivorMemoryPool = memoryPool;
		} else if (lowerCaseName.contains(EDEN)) {
			edenMemoryPool = memoryPool;
		} else if (lowerCaseName.contains(OLD) || lowerCaseName.contains(TENURED)) {
			oldMemoryPool = memoryPool;
		} else if (lowerCaseName.contains(PERM) || lowerCaseName.contains(METASPACE)) {
			permMemoryPool = memoryPool;
		} else if (lowerCaseName.contains(CODE_CACHE)) {
			codeCacheMemoryPool = memoryPool;
		} else if (lowerCaseName.contains(COMPRESSED_CLASS_SPACE)) {
			compressedClassSpaceMemoryPool = memoryPool;
		}
	}
}
 
Example #12
Source File: JvmMemMgrPoolRelTableMetaImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a map pool-name => pool-index from the SnmpTableHandler
 * of the JvmMemPoolTable.
 **/
private static Map<String, SnmpOid> buildPoolIndexMap(SnmpTableHandler handler) {
    // optimization...
    if (handler instanceof SnmpCachedData)
        return buildPoolIndexMap((SnmpCachedData)handler);

    // not optimizable... too bad.
    final Map<String, SnmpOid> m = new HashMap<>();
    SnmpOid index=null;
    while ((index = handler.getNext(index))!=null) {
        final MemoryPoolMXBean mpm =
            (MemoryPoolMXBean)handler.getData(index);
        if (mpm == null) continue;
        final String name = mpm.getName();
        if (name == null) continue;
        m.put(name,index);
    }
    return m;
}
 
Example #13
Source File: JvmMemMgrPoolRelTableMetaImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a map pool-name => pool-index from the SnmpTableHandler
 * of the JvmMemPoolTable.
 **/
private static Map<String, SnmpOid> buildPoolIndexMap(SnmpTableHandler handler) {
    // optimization...
    if (handler instanceof SnmpCachedData)
        return buildPoolIndexMap((SnmpCachedData)handler);

    // not optimizable... too bad.
    final Map<String, SnmpOid> m = new HashMap<>();
    SnmpOid index=null;
    while ((index = handler.getNext(index))!=null) {
        final MemoryPoolMXBean mpm =
            (MemoryPoolMXBean)handler.getData(index);
        if (mpm == null) continue;
        final String name = mpm.getName();
        if (name == null) continue;
        m.put(name,index);
    }
    return m;
}
 
Example #14
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 #15
Source File: MemoryPoolMeter.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Iterable<Measurement> measure() {
  final long timestamp = clock.wallTime();
  final MemoryPoolMXBean mbean = ref.get();
  final List<Measurement> ms = new ArrayList<>();
  if (mbean != null) {
    final String typeKey = "memtype";
    final String type = mbean.getType().name();

    final MemoryUsage usage = mbean.getUsage();
    ms.add(new Measurement(usedId.withTag(typeKey, type), timestamp, usage.getUsed()));
    ms.add(new Measurement(committedId.withTag(typeKey, type), timestamp, usage.getCommitted()));
    ms.add(new Measurement(maxId.withTag(typeKey, type), timestamp, usage.getMax()));
  }
  return ms;
}
 
Example #16
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 #17
Source File: MemoryManagerImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public String[] getMemoryPoolNames() {
    MemoryPoolMXBean[] ps = getMemoryPools();

    String[] names = new String[ps.length];
    for (int i = 0; i < ps.length; i++) {
        names[i] = ps[i].getName();
    }
    return names;
}
 
Example #18
Source File: JvmMemoryMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
private MemoryPoolMXBean getCodeCacheMXBean() {
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        if (CODE_CACHE_NAME.equals(memoryPoolMXBean.getName())) {
            return memoryPoolMXBean;
        }
    }

    return null;
}
 
Example #19
Source File: MemoryUtils.java    From reef with Apache License 2.0 5 votes vote down vote up
private static long peakMemoryUsage(final String name) {
  final List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
  for (final MemoryPoolMXBean bean : memoryPoolMXBeans) {
    if (bean.getName().toLowerCase().indexOf(name) != -1) {
      return bean.getPeakUsage().getUsed() / BYTES_IN_MEGABYTE;
    }
  }
  return 0;
}
 
Example #20
Source File: HeapMonitorThread.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void checkAndClawBackHeap() throws InterruptedException {
  // verify that at-least one of the pools has exceeded the collection threshold.
  boolean exceeded = false;
  for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
    if (!monitoredPools.containsKey(pool.getName())) {
      continue;
    }

    long thresholdExceededCount = pool.getCollectionUsageThresholdCount();
    if (monitoredPools.get(pool.getName()) < thresholdExceededCount) {
      monitoredPools.put(pool.getName(), thresholdExceededCount);
      exceeded = true;

      logger.info("heap usage " + pool.getUsage().getUsed() +
          " in pool " + pool.getName() +
          " exceeded threshold " + pool.getCollectionUsageThreshold() +
          " threshold_cnt " + pool.getCollectionUsageThresholdCount());
    }
  }
  if (exceeded) {
    strategy.clawBack();
  } else {
    logger.info("spurious wakeup");
  }

  // block for a while to let the cancel do it's work.
  Thread.sleep(1000);
}
 
Example #21
Source File: LastGCInfo.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkGcInfo(String name, GcInfo info) throws Exception {
    System.out.println("GC statistic for : " + name);
    System.out.print("GC #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map usage = info.getMemoryUsageBeforeGc();

    List pnames = new ArrayList();
    for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iter.next();
        String poolname = (String) entry.getKey();
        pnames.add(poolname);
        MemoryUsage busage = (MemoryUsage) entry.getValue();
        MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
        if (ausage == null) {
            throw new RuntimeException("After Gc Memory does not exist" +
                " for " + poolname);
        }
        System.out.println("Usage for pool " + poolname);
        System.out.println("   Before GC: " + busage);
        System.out.println("   After GC: " + ausage);
    }

    // check if memory usage for all memory pools are returned
    List pools = ManagementFactory.getMemoryPoolMXBeans();
    for (Iterator iter = pools.iterator(); iter.hasNext(); ) {
        MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
        if (!pnames.contains(p.getName())) {
            throw new RuntimeException("GcInfo does not contain " +
                "memory usage for pool " + p.getName());
        }
    }
}
 
Example #22
Source File: TestCodeSweeper.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static boolean canAllocate(double size, long maxSize, MemoryPoolMXBean bean) {
    // Don't fill too much to have space for adapters. So, stop after crossing 95% and
    // don't allocate in case we'll cross 97% on next allocation.
    double used = bean.getUsage().getUsed();
    return (used <= CACHE_USAGE_COEF * maxSize) &&
           (used + size <= (CACHE_USAGE_COEF + 0.02d)  * maxSize);
}
 
Example #23
Source File: MemoryPoolModule.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public List<MemoryPool> getMemoryPoolMetricsList() {
    List<MemoryPool> poolList = new LinkedList<MemoryPool>();
    for (MemoryPoolMXBean bean : beans) {
        String name = bean.getName();
        PoolType type;
        if (contains(getCodeCacheNames(), name)) {
            type = PoolType.CODE_CACHE_USAGE;
        } else if (contains(getEdenNames(), name)) {
            type = PoolType.NEWGEN_USAGE;
        } else if (contains(getOldNames(), name)) {
            type = PoolType.OLDGEN_USAGE;
        } else if (contains(getSurvivorNames(), name)) {
            type = PoolType.SURVIVOR_USAGE;
        } else if (contains(getMetaspaceNames(), name)) {
            type = PoolType.METASPACE_USAGE;
        } else if (contains(getPermNames(), name)) {
            type = PoolType.PERMGEN_USAGE;
        } else {
            continue;
        }

        MemoryUsage usage = bean.getUsage();
        poolList.add(MemoryPool.newBuilder()
                               .setType(type)
                               .setInit(usage.getInit())
                               .setMax(usage.getMax())
                               .setCommitted(usage.getCommitted())
                               .setUsed(usage.getUsed())
                               .build());
    }
    return poolList;
}
 
Example #24
Source File: MonitoredDataImpl.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
MonitoredDataImpl(JmxSupport jmxSupport,JvmMXBeans jmxModel) {
  this(jmxSupport);
  RuntimeMXBean runtimeBean = jmxModel.getRuntimeMXBean();
  upTime = runtimeBean.getUptime();
  ClassLoadingMXBean classBean = jmxModel.getClassLoadingMXBean();
  ThreadMXBean threadBean = jmxModel.getThreadMXBean();
  MemoryUsage mem = jmxModel.getMemoryMXBean().getHeapMemoryUsage();
  MemoryPoolMXBean permBean = jmxSupport.getPermGenPool();
  unloadedClasses = classBean.getUnloadedClassCount();
  loadedClasses = classBean.getLoadedClassCount() + unloadedClasses;
  sharedLoadedClasses = 0;
  sharedUnloadedClasses = 0;
  threadsDaemon = threadBean.getDaemonThreadCount();
  threadsLive = threadBean.getThreadCount();
  threadsLivePeak = threadBean.getPeakThreadCount();
  threadsStarted = threadBean.getTotalStartedThreadCount();
  applicationTime = 0;
  genCapacity = new long[2];
  genUsed = new long[2];
  genMaxCapacity = new long[2];
  genCapacity[0] = mem.getCommitted();
  genUsed[0] = mem.getUsed();
  genMaxCapacity[0] = mem.getMax();
  if (permBean != null) {
      MemoryUsage perm = permBean.getUsage();
      genCapacity[1] = perm.getCommitted();
      genUsed[1] = perm.getUsed();
      genMaxCapacity[1] = perm.getMax();
  }
}
 
Example #25
Source File: LastGCInfo.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkGcInfo(String name, GcInfo info) throws Exception {
    System.out.println("GC statistic for : " + name);
    System.out.print("GC #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map usage = info.getMemoryUsageBeforeGc();

    List pnames = new ArrayList();
    for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iter.next();
        String poolname = (String) entry.getKey();
        pnames.add(poolname);
        MemoryUsage busage = (MemoryUsage) entry.getValue();
        MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
        if (ausage == null) {
            throw new RuntimeException("After Gc Memory does not exist" +
                " for " + poolname);
        }
        System.out.println("Usage for pool " + poolname);
        System.out.println("   Before GC: " + busage);
        System.out.println("   After GC: " + ausage);
    }

    // check if memory usage for all memory pools are returned
    List pools = ManagementFactory.getMemoryPoolMXBeans();
    for (Iterator iter = pools.iterator(); iter.hasNext(); ) {
        MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
        if (!pnames.contains(p.getName())) {
            throw new RuntimeException("GcInfo does not contain " +
                "memory usage for pool " + p.getName());
        }
    }
}
 
Example #26
Source File: MemoryMonitor.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void setPercentageUsageThreshold(double percentage) {
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
            if (percentage <= 0.0 || percentage > 1.0) {
                throw new IllegalArgumentException("percentage not in range");
            }
            long warningThreshold = (long) (pool.getUsage().getMax() * percentage);
            pool.setUsageThreshold(warningThreshold);
        }
    }
}
 
Example #27
Source File: Pools.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static long getPoolCommittedSize(String poolName) {
    long result;
    MemoryPoolMXBean pool = findPool(poolName);
    if (pool != null) {
        if (pool.getUsage().getCommitted() == -1) {
            result = -1;
        } else {
            result = pool.getUsage().getCommitted() / 1024;
        }
    } else {
        throw new RuntimeException("Pool '" + poolName + "' wasn't found");
    }
    log("Committed size of the pool '" + poolName + "' is " + result);
    return result;
}
 
Example #28
Source File: MemoryManagerImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public String[] getMemoryPoolNames() {
    MemoryPoolMXBean[] ps = getMemoryPools();

    String[] names = new String[ps.length];
    for (int i = 0; i < ps.length; i++) {
        names[i] = ps[i].getName();
    }
    return names;
}
 
Example #29
Source File: MXBeanStatistics.java    From garmadon with Apache License 2.0 5 votes vote down vote up
protected void addMemoryStatistics(StatisticCollector collector) {
    collector.register(new MemoryUsageStatistics(HEAP_HEADER, ManagementFactory.getMemoryMXBean()::getHeapMemoryUsage));
    collector.register(new MemoryUsageStatistics(NON_HEAP_HEADER, ManagementFactory.getMemoryMXBean()::getNonHeapMemoryUsage));
    MemoryPoolMXBean[] memPools = ManagementFactory.getMemoryPoolMXBeans().toArray(new MemoryPoolMXBean[0]);
    for (MemoryPoolMXBean memPool : memPools) {
        String poolName = MXBeanHelper.normalizeName(memPool.getName());
        collector.register(new MemoryUsageStatistics(poolName, memPool::getUsage));
    }
}
 
Example #30
Source File: MemoryPoolSensor.java    From swage with Apache License 2.0 5 votes vote down vote up
@Override
public void sense(final MetricContext metricContext)
{
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();

    for (MemoryPoolMXBean mxBean : pools) {
        reportUsage(mxBean, metricContext);
    }

}