JVM Integration for Metrics - A set of classes which allow you to monitor critical aspects of your Java Virtual Machine using Metrics
—
Buffer pool usage statistics for direct and mapped buffers, providing insight into off-heap memory usage. Available on Java 7 and above.
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.MetricRegistry;
import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;Monitors JVM buffer pools including direct and mapped buffer usage statistics.
/**
* A set of gauges for the count, usage, and capacity of the JVM's direct and mapped buffer pools.
* These JMX objects are only available on Java 7 and above.
*/
public class BufferPoolMetricSet implements MetricSet {
/**
* Creates a new set of buffer pool gauges.
* @param mBeanServer the MBeanServer to query for buffer pool statistics
*/
public BufferPoolMetricSet(MBeanServer mBeanServer);
/**
* Returns all buffer pool metrics.
* @return map of metric names to Metric instances
*/
public Map<String, Metric> getMetrics();
}Metrics Provided:
For each buffer pool type (direct and mapped):
{pool-type}.count - Number of buffers in the pool{pool-type}.used - Memory used by buffers in bytes{pool-type}.capacity - Total capacity of buffers in bytesSpecific metrics:
direct.count, direct.used, direct.capacity - Direct buffer statisticsmapped.count, mapped.used, mapped.capacity - Mapped buffer statisticsUsage Examples:
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import java.lang.management.ManagementFactory;
// Basic usage with platform MBean server
MetricRegistry registry = new MetricRegistry();
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
registry.registerAll(new BufferPoolMetricSet(server));
// Access buffer pool metrics
Gauge<Long> directCount = registry.getGauges().get("direct.count");
Gauge<Long> directUsed = registry.getGauges().get("direct.used");
Gauge<Long> directCapacity = registry.getGauges().get("direct.capacity");
Gauge<Long> mappedCount = registry.getGauges().get("mapped.count");
Gauge<Long> mappedUsed = registry.getGauges().get("mapped.used");
Gauge<Long> mappedCapacity = registry.getGauges().get("mapped.capacity");
// Display direct buffer statistics
if (directCount != null) {
System.out.println("Direct buffers: " + directCount.getValue());
System.out.println("Direct buffer memory used: " + directUsed.getValue() + " bytes");
System.out.println("Direct buffer capacity: " + directCapacity.getValue() + " bytes");
// Calculate utilization ratio
double directUtilization = directCapacity.getValue() > 0 ?
(double) directUsed.getValue() / directCapacity.getValue() : 0.0;
System.out.println("Direct buffer utilization: " + (directUtilization * 100) + "%");
}
// Display mapped buffer statistics
if (mappedCount != null) {
System.out.println("Mapped buffers: " + mappedCount.getValue());
System.out.println("Mapped buffer memory used: " + mappedUsed.getValue() + " bytes");
System.out.println("Mapped buffer capacity: " + mappedCapacity.getValue() + " bytes");
}
// Custom MBean server
MBeanServer customServer = MBeanServerFactory.createMBeanServer();
registry.registerAll(new BufferPoolMetricSet(customServer));Buffer Pool Types:
Direct Buffers: Off-heap memory allocated using ByteBuffer.allocateDirect() or similar methods
Mapped Buffers: Memory-mapped file buffers created using FileChannel.map()
Monitoring Considerations:
Platform Compatibility:
Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard-metrics--metrics-jvm