Example usage for java.lang.management BufferPoolMXBean getCount

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

Introduction

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

Prototype

long getCount();

Source Link

Document

Returns an estimate of the number of buffers in the 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//from  w w  w.  j  a  va 2s  . c  o m
            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/*w  w w .j av  a  2  s.c  o  m*/
        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();
            }
        });
    }
}