Java Code Examples for java.lang.management.MemoryPoolMXBean#getUsage()
The following examples show how to use
java.lang.management.MemoryPoolMXBean#getUsage() .
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: MemoryInformations.java From javamelody with Apache License 2.0 | 6 votes |
MemoryInformations() { super(); usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); maxMemory = Runtime.getRuntime().maxMemory(); final MemoryPoolMXBean permGenMemoryPool = getPermGenMemoryPool(); if (permGenMemoryPool != null) { final MemoryUsage usage = permGenMemoryPool.getUsage(); usedPermGen = usage.getUsed(); maxPermGen = usage.getMax(); } else { usedPermGen = -1; maxPermGen = -1; } usedNonHeapMemory = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed(); usedBufferedMemory = MBeansAccessor.getUsedBufferMemory(); loadedClassesCount = ManagementFactory.getClassLoadingMXBean().getLoadedClassCount(); garbageCollectionTimeMillis = buildGarbageCollectionTimeMillis(); usedPhysicalMemorySize = MBeansAccessor .getLongFromOperatingSystem("TotalPhysicalMemorySize") - MBeansAccessor.getLongFromOperatingSystem("FreePhysicalMemorySize"); usedSwapSpaceSize = buildUsedSwapSpaceSize(); memoryDetails = buildMemoryDetails(); }
Example 2
Source File: Memory.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
/** * 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 3
Source File: MemoryWatchdog.java From pitest with Apache License 2.0 | 6 votes |
public static void addWatchDogToAllPools(final long threshold, final NotificationListener listener) { final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); final NotificationEmitter ne = (NotificationEmitter) memBean; ne.addNotificationListener(listener, null, null); final List<MemoryPoolMXBean> memPools = ManagementFactory .getMemoryPoolMXBeans(); for (final MemoryPoolMXBean mp : memPools) { if (mp.isUsageThresholdSupported()) { final MemoryUsage mu = mp.getUsage(); final long max = mu.getMax(); final long alert = (max * threshold) / 100; // LOG.info("Setting a threshold shutdown on pool: " + mp.getName() // + " for: " + alert); mp.setUsageThreshold(alert); } } }
Example 4
Source File: GarbageProducer.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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 5
Source File: MemoryPoolSensor.java From swage with Apache License 2.0 | 6 votes |
private void reportUsage(MemoryPoolMXBean mxBean, MetricContext metricContext) { String name = mxBean.getName(); Metric usedMetric = Metric.define("MemoryPoolUsed_" + name); Metric maxMetric = Metric.define("MemoryPoolMax_" + name); Metric percMetric = Metric.define("MemoryPoolUsage_" + name); MemoryUsage usage = mxBean.getUsage(); long used = usage.getUsed(); long max = usage.getMax(); metricContext.record(usedMetric, used / M, Unit.MEGABYTE); // max can be undefined (-1) https://docs.oracle.com/javase/8/docs/api/java/lang/management/MemoryUsage.html if (max >= 0) { metricContext.record(maxMetric, max / M, Unit.MEGABYTE); double used_percent = 100.0 * ((double)used/(double)max); metricContext.record(percMetric, used_percent, Unit.PERCENT); } }
Example 6
Source File: Monitors.java From sumk with Apache License 2.0 | 6 votes |
public static String jvmInfo() { StringBuilder sb = new StringBuilder(); sb.append("## name init max commited used").append(LN); DecimalFormat f = new DecimalFormat("#,###"); List<MemoryPoolMXBean> mpmxbs = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean mpmxb : mpmxbs) { if (mpmxb == null || mpmxb.getUsage() == null) { continue; } String name = mpmxb.getName(); if (name == null || name.isEmpty()) { continue; } sb.append(name).append(BLANK).append(f.format(mpmxb.getUsage().getInit())).append(BLANK) .append(f.format(mpmxb.getUsage().getMax())).append(BLANK) .append(f.format(mpmxb.getUsage().getCommitted())).append(BLANK) .append(f.format(mpmxb.getUsage().getUsed())).append(LN); } return sb.toString(); }
Example 7
Source File: DefaultDetailedMemoryMetric.java From pinpoint with Apache License 2.0 | 6 votes |
private static MemoryPoolMXBeanWrapper wrap(final MemoryPoolMXBean memoryPoolMXBean) { if (memoryPoolMXBean == null) { return new MemoryPoolMXBeanWrapper() { @Override public MemoryUsage getUsage() { return null; } }; } else { return new MemoryPoolMXBeanWrapper() { @Override public MemoryUsage getUsage() { return memoryPoolMXBean.getUsage(); } }; } }
Example 8
Source File: MemoryPoolMXBeanMetricFamilyCollector.java From cassandra-exporter with Apache License 2.0 | 6 votes |
@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 9
Source File: MemoryLogger.java From flink with Apache License 2.0 | 6 votes |
/** * Gets the memory pool statistics from the JVM. * * @param poolBeans The collection of memory pool beans. * @return A string denoting the names and sizes of the memory pools. */ public static String getMemoryPoolStatsAsString(List<MemoryPoolMXBean> poolBeans) { StringBuilder bld = new StringBuilder("Off-heap pool stats: "); int count = 0; for (MemoryPoolMXBean bean : poolBeans) { if (bean.getType() == MemoryType.NON_HEAP) { if (count > 0) { bld.append(", "); } count++; MemoryUsage usage = bean.getUsage(); long used = usage.getUsed() >> 20; long committed = usage.getCommitted() >> 20; long max = usage.getMax() >> 20; bld.append('[').append(bean.getName()).append(": "); bld.append(used).append('/').append(committed).append('/').append(max); bld.append(" MB (used/committed/max)]"); } } return bld.toString(); }
Example 10
Source File: MemoryLogger.java From flink with Apache License 2.0 | 6 votes |
/** * Gets the memory pool statistics from the JVM. * * @param poolBeans The collection of memory pool beans. * @return A string denoting the names and sizes of the memory pools. */ public static String getMemoryPoolStatsAsString(List<MemoryPoolMXBean> poolBeans) { StringBuilder bld = new StringBuilder("Off-heap pool stats: "); int count = 0; for (MemoryPoolMXBean bean : poolBeans) { if (bean.getType() == MemoryType.NON_HEAP) { if (count > 0) { bld.append(", "); } count++; MemoryUsage usage = bean.getUsage(); long used = usage.getUsed() >> 20; long committed = usage.getCommitted() >> 20; long max = usage.getMax() >> 20; bld.append('[').append(bean.getName()).append(": "); bld.append(used).append('/').append(committed).append('/').append(max); bld.append(" MB (used/committed/max)]"); } } return bld.toString(); }
Example 11
Source File: MemoryPoolMeter.java From spectator with Apache License 2.0 | 5 votes |
@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 12
Source File: MonitoredDataImpl.java From visualvm with GNU General Public License v2.0 | 5 votes |
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 13
Source File: HeapRegionUsageTool.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Get MemoryUsage from MemoryPoolMXBean which name matches passed string. * * @param name * @return MemoryUsage */ private static MemoryUsage getUsage(String name){ for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { if (pool.getName().matches(name)) { return pool.getUsage(); } } return null; }
Example 14
Source File: MemoryPoolsExports.java From client_java with Apache License 2.0 | 5 votes |
void addMemoryPoolMetrics(List<MetricFamilySamples> sampleFamilies) { GaugeMetricFamily used = new GaugeMetricFamily( "jvm_memory_pool_bytes_used", "Used bytes of a given JVM memory pool.", Collections.singletonList("pool")); sampleFamilies.add(used); GaugeMetricFamily committed = new GaugeMetricFamily( "jvm_memory_pool_bytes_committed", "Committed bytes of a given JVM memory pool.", Collections.singletonList("pool")); sampleFamilies.add(committed); GaugeMetricFamily max = new GaugeMetricFamily( "jvm_memory_pool_bytes_max", "Max bytes of a given JVM memory pool.", Collections.singletonList("pool")); sampleFamilies.add(max); GaugeMetricFamily init = new GaugeMetricFamily( "jvm_memory_pool_bytes_init", "Initial bytes of a given JVM memory pool.", Collections.singletonList("pool")); sampleFamilies.add(init); for (final MemoryPoolMXBean pool : poolBeans) { MemoryUsage poolUsage = pool.getUsage(); used.addMetric( Collections.singletonList(pool.getName()), poolUsage.getUsed()); committed.addMetric( Collections.singletonList(pool.getName()), poolUsage.getCommitted()); max.addMetric( Collections.singletonList(pool.getName()), poolUsage.getMax()); init.addMetric( Collections.singletonList(pool.getName()), poolUsage.getInit()); } }
Example 15
Source File: MemoryPoolModule.java From skywalking with Apache License 2.0 | 5 votes |
@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 16
Source File: Fawe.java From FastAsyncWorldedit with GNU General Public License v3.0 | 4 votes |
private void setupMemoryListener() { if (Settings.IMP.MAX_MEMORY_PERCENT < 1 || Settings.IMP.MAX_MEMORY_PERCENT > 99) { return; } try { final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); final NotificationEmitter ne = (NotificationEmitter) memBean; ne.addNotificationListener(new NotificationListener() { @Override public void handleNotification(final Notification notification, final Object handback) { final long heapSize = Runtime.getRuntime().totalMemory(); final long heapMaxSize = Runtime.getRuntime().maxMemory(); if (heapSize < heapMaxSize) { return; } MemUtil.memoryLimitedTask(); } }, null, null); final List<MemoryPoolMXBean> memPools = ManagementFactory.getMemoryPoolMXBeans(); for (final MemoryPoolMXBean mp : memPools) { if (mp.isUsageThresholdSupported()) { final MemoryUsage mu = mp.getUsage(); final long max = mu.getMax(); if (max < 0) { continue; } final long alert = (max * Settings.IMP.MAX_MEMORY_PERCENT) / 100; mp.setUsageThreshold(alert); } } } catch (Throwable e) { debug("====== MEMORY LISTENER ERROR ======"); MainUtil.handleError(e, false); debug("==================================="); debug("FAWE needs access to the JVM memory system:"); debug(" - Change your Java security settings"); debug(" - Disable this with `max-memory-percent: -1`"); debug("==================================="); } }
Example 17
Source File: SmallRyeMetricsRecorder.java From quarkus with Apache License 2.0 | 4 votes |
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 18
Source File: JVMToolHelper.java From uavstack with Apache License 2.0 | 4 votes |
public static Map<String, Long> readHeapPoolUsage(List<MemoryPoolMXBean> pmbList) { Map<String, Long> m = new LinkedHashMap<String, Long>(); /*for (MemoryPoolMXBean mpmb : pmbList) { String jvmMemPoolName = getHeapPoolName(mpmb.getName().trim()); MemoryUsage mu = mpmb.getUsage(); m.put(jvmMemPoolName + "_use", mu.getUsed()); m.put(jvmMemPoolName + "_commit", mu.getCommitted()); m.put(jvmMemPoolName + "_max", mu.getMax()); m.put(jvmMemPoolName + "_init", mu.getInit()); }*/ Set<String> addedSet = new HashSet<String>(); for (MemoryPoolMXBean mpmb : pmbList) { String jvmMemPoolName = getHeapPoolName(mpmb.getName().trim()); MemoryUsage mu = mpmb.getUsage(); if(addedSet.contains(jvmMemPoolName)) { m.put(jvmMemPoolName + "_use", (Long)m.get(jvmMemPoolName + "_use") + mu.getUsed()); m.put(jvmMemPoolName + "_commit", (Long)m.get(jvmMemPoolName + "_commit") + mu.getCommitted()); m.put(jvmMemPoolName + "_max", (Long)m.get(jvmMemPoolName + "_max") + mu.getMax()); m.put(jvmMemPoolName + "_init", (Long)m.get(jvmMemPoolName + "_init") + mu.getInit()); }else { addedSet.add(jvmMemPoolName); m.put(jvmMemPoolName + "_use", mu.getUsed()); m.put(jvmMemPoolName + "_commit", mu.getCommitted()); m.put(jvmMemPoolName + "_max", mu.getMax()); m.put(jvmMemPoolName + "_init", mu.getInit()); } } return m; }
Example 19
Source File: JvmStats.java From crate with Apache License 2.0 | 4 votes |
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); }
Example 20
Source File: JvmMem.java From OSPREY3 with GNU General Public License v2.0 | 4 votes |
public MemInfo(MemoryPoolMXBean pool) { this(pool.getName(), pool.getUsage()); }