CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-dropwizard-metrics--metrics-jvm

JVM Integration for Metrics - A set of classes which allow you to monitor critical aspects of your Java Virtual Machine using Metrics

Pending
Overview
Eval results
Files

buffer-pool-monitoring.mddocs/

Buffer Pool Monitoring

Buffer pool usage statistics for direct and mapped buffers, providing insight into off-heap memory usage. Available on Java 7 and above.

Core Imports

import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.MetricRegistry;
import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;

Capabilities

BufferPoolMetricSet

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 bytes

Specific metrics:

  • direct.count, direct.used, direct.capacity - Direct buffer statistics
  • mapped.count, mapped.used, mapped.capacity - Mapped buffer statistics

Usage 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

    • Used for NIO operations, networking, and memory-mapped files
    • Not subject to GC but limited by available system memory
    • Freed when the buffer object is garbage collected
  • Mapped Buffers: Memory-mapped file buffers created using FileChannel.map()

    • Backed by files on disk but cached in memory
    • Provide efficient file I/O through virtual memory mapping
    • Memory usage depends on file size and OS caching behavior

Monitoring Considerations:

  • High direct buffer usage may indicate memory leaks or excessive off-heap allocation
  • Growing buffer counts without corresponding decreases may suggest reference leaks
  • Capacity vs. used ratios help identify buffer pool efficiency
  • Missing metrics on older Java versions (< 7) will result in empty metric maps

Platform Compatibility:

  • Requires Java 7 or higher for JMX buffer pool support
  • On Java 6 or when buffer pool MBeans are unavailable, metrics will be empty
  • Different JVM implementations may expose different buffer pool types

Install with Tessl CLI

npx tessl i tessl/maven-io-dropwizard-metrics--metrics-jvm

docs

buffer-pool-monitoring.md

class-loading-monitoring.md

cpu-time-monitoring.md

file-descriptor-monitoring.md

gc-monitoring.md

index.md

jmx-attribute-access.md

jvm-attributes.md

memory-monitoring.md

thread-monitoring.md

tile.json