Example usage for java.lang.management BufferPoolMXBean getMemoryUsed

List of usage examples for java.lang.management BufferPoolMXBean getMemoryUsed

Introduction

In this page you can find the example usage for java.lang.management BufferPoolMXBean getMemoryUsed.

Prototype

long getMemoryUsed();

Source Link

Document

Returns an estimate of the memory that the Java virtual machine is using for this buffer pool.

Usage

From source file:net.centro.rtb.monitoringcenter.metrics.system.jvm.BufferPoolMetricSet.java

BufferPoolMetricSet() {
    this.bufferPoolMXBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);

    List<BufferPoolStatus> bufferPoolStatuses = new ArrayList<>();
    Map<String, Metric> metricsByNames = new HashMap<>();

    for (final BufferPoolMXBean bufferPoolMXBean : bufferPoolMXBeans) {
        final String bufferPoolName = bufferPoolMXBean.getName();

        final Gauge<Long> sizeGauge = new Gauge<Long>() {
            @Override//  w  ww . j a va2s  .c om
            public Long getValue() {
                return bufferPoolMXBean.getCount();
            }
        };
        metricsByNames.put(MetricNamingUtil.join(bufferPoolName, "size"), sizeGauge);

        final Gauge<Long> totalCapacityInBytesGauge = new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferPoolMXBean.getTotalCapacity();
            }
        };
        metricsByNames.put(MetricNamingUtil.join(bufferPoolName, "totalCapacityInBytes"),
                totalCapacityInBytesGauge);

        final Gauge<Long> usedMemoryInBytesGauge;
        if (bufferPoolMXBean.getMemoryUsed() >= 0) {
            usedMemoryInBytesGauge = new Gauge<Long>() {
                @Override
                public Long getValue() {
                    return bufferPoolMXBean.getMemoryUsed();
                }
            };
            metricsByNames.put(MetricNamingUtil.join(bufferPoolName, "usedMemoryInBytes"),
                    usedMemoryInBytesGauge);
        } else {
            usedMemoryInBytesGauge = null;
        }

        bufferPoolStatuses.add(new BufferPoolStatus() {
            @Override
            public String getName() {
                return bufferPoolName;
            }

            @Override
            public Gauge<Long> getSizeGauge() {
                return sizeGauge;
            }

            @Override
            public Gauge<Long> getTotalCapacityInBytesGauge() {
                return totalCapacityInBytesGauge;
            }

            @Override
            public Gauge<Long> getUsedMemoryInBytesGauge() {
                return usedMemoryInBytesGauge;
            }
        });
    }

    this.bufferPoolStatuses = bufferPoolStatuses;
    this.metricsByNames = metricsByNames;
}

From source file:org.apache.flink.runtime.metrics.util.MetricUtils.java

private static void instantiateMemoryMetrics(MetricGroup metrics) {
    final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
    MetricGroup heap = metrics.addGroup("Heap");
    heap.gauge("Used", new Gauge<Long>() {
        @Override/*from  w ww.j  a v a2 s  .  com*/
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getUsed();
        }
    });
    heap.gauge("Committed", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted();
        }
    });
    heap.gauge("Max", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax();
        }
    });

    MetricGroup nonHeap = metrics.addGroup("NonHeap");
    nonHeap.gauge("Used", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    nonHeap.gauge("Committed", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    nonHeap.gauge("Max", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getMax();
        }
    });

    List<BufferPoolMXBean> bufferMxBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);

    for (final BufferPoolMXBean bufferMxBean : bufferMxBeans) {
        MetricGroup bufferGroup = metrics.addGroup(WordUtils.capitalize(bufferMxBean.getName()));
        bufferGroup.gauge("Count", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferMxBean.getCount();
            }
        });
        bufferGroup.gauge("MemoryUsed", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferMxBean.getMemoryUsed();
            }
        });
        bufferGroup.gauge("TotalCapacity", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferMxBean.getTotalCapacity();
            }
        });
    }
}

From source file:org.apache.pulsar.common.stats.JvmMetrics.java

public static long getJvmDirectMemoryUsed() {
    if (directMemoryUsage != null) {
        try {/*w  w  w  .  jav  a 2s. c  o m*/
            return ((AtomicLong) directMemoryUsage.get(null)).get();
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to get netty-direct-memory used count {}", e.getMessage());
            }
        }
    }

    List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    for (BufferPoolMXBean pool : pools) {
        if (pool.getName().equals("direct")) {
            return pool.getMemoryUsed();
        }
    }

    // Couldnt get direct memory usage
    return -1;
}

From source file:org.apache.tajo.QueryTestCaseBase.java

@Before
public void printTestName() {
    /* protect a travis stalled build */
    BufferPoolMXBean direct = BufferPool.getDirectBufferPool();
    BufferPoolMXBean mapped = BufferPool.getMappedBufferPool();
    System.out.println(/*from ww w.j  ava2  s . c o  m*/
            String.format("Used heap: %s/%s, direct:%s/%s, mapped:%s/%s, Active Threads: %d, Run: %s.%s",
                    FileUtil.humanReadableByteCount(
                            Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(), false),
                    FileUtil.humanReadableByteCount(Runtime.getRuntime().maxMemory(), false),
                    FileUtil.humanReadableByteCount(direct.getMemoryUsed(), false),
                    FileUtil.humanReadableByteCount(direct.getTotalCapacity(), false),
                    FileUtil.humanReadableByteCount(mapped.getMemoryUsed(), false),
                    FileUtil.humanReadableByteCount(mapped.getTotalCapacity(), false), Thread.activeCount(),
                    getClass().getSimpleName(), name.getMethodName()));
}