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

gc-monitoring.mddocs/

Garbage Collection Monitoring

Garbage collection statistics including collection counts and elapsed times for all discoverable garbage collectors in the JVM.

Core Imports

import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.MetricRegistry;
import java.lang.management.GarbageCollectorMXBean;

Capabilities

GarbageCollectorMetricSet

Provides comprehensive garbage collection metrics for monitoring GC performance and behavior.

/**
 * A set of gauges for the counts and elapsed times of garbage collections.
 */
public class GarbageCollectorMetricSet implements MetricSet {
    /**
     * Creates a new set of gauges for all discoverable garbage collectors.
     */
    public GarbageCollectorMetricSet();
    
    /**
     * Creates a new set of gauges for the given collection of garbage collectors.
     * @param garbageCollectors the garbage collectors to monitor
     */
    public GarbageCollectorMetricSet(Collection<GarbageCollectorMXBean> garbageCollectors);
    
    /**
     * Returns all garbage collection metrics.
     * @return map of metric names to Metric instances
     */
    public Map<String, Metric> getMetrics();
}

Metrics Provided:

For each garbage collector, two metrics are provided:

  • {gc-name}.count - Total number of collections performed
  • {gc-name}.time - Total time spent in collections (milliseconds)

Common garbage collector names (whitespace normalized to hyphens):

  • PS-Scavenge.count / PS-Scavenge.time - Parallel Scavenge young generation collector
  • PS-MarkSweep.count / PS-MarkSweep.time - Parallel Mark Sweep old generation collector
  • G1-Young-Generation.count / G1-Young-Generation.time - G1 young generation collector
  • G1-Old-Generation.count / G1-Old-Generation.time - G1 old generation collector
  • ConcurrentMarkSweep.count / ConcurrentMarkSweep.time - CMS collector
  • ParNew.count / ParNew.time - Parallel New young generation collector

Usage Examples:

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import java.lang.management.ManagementFactory;

// Basic usage with all discoverable GC beans
MetricRegistry registry = new MetricRegistry();
registry.registerAll(new GarbageCollectorMetricSet());

// Access GC metrics
Gauge<Long> youngGcCount = registry.getGauges().get("PS-Scavenge.count");
Gauge<Long> youngGcTime = registry.getGauges().get("PS-Scavenge.time");
Gauge<Long> oldGcCount = registry.getGauges().get("PS-MarkSweep.count");
Gauge<Long> oldGcTime = registry.getGauges().get("PS-MarkSweep.time");

if (youngGcCount != null) {
    System.out.println("Young GC collections: " + youngGcCount.getValue());
    System.out.println("Young GC time: " + youngGcTime.getValue() + " ms");
}

if (oldGcCount != null) {
    System.out.println("Old GC collections: " + oldGcCount.getValue());  
    System.out.println("Old GC time: " + oldGcTime.getValue() + " ms");
}

// Custom GC bean collection
Collection<GarbageCollectorMXBean> specificCollectors = ManagementFactory.getGarbageCollectorMXBeans()
    .stream()
    .filter(gc -> gc.getName().contains("G1"))
    .collect(Collectors.toList());
registry.registerAll(new GarbageCollectorMetricSet(specificCollectors));

// Calculate GC frequency and throughput
long totalCollections = registry.getGauges().values().stream()
    .filter(gauge -> gauge instanceof Gauge)
    .map(gauge -> (Gauge<Long>) gauge)
    .filter(gauge -> registry.getGauges()
        .entrySet()
        .stream()
        .anyMatch(entry -> entry.getKey().endsWith(".count") && entry.getValue() == gauge))
    .mapToLong(Gauge::getValue)
    .sum();

long totalGcTime = registry.getGauges().values().stream()
    .filter(gauge -> gauge instanceof Gauge)
    .map(gauge -> (Gauge<Long>) gauge)
    .filter(gauge -> registry.getGauges()
        .entrySet()
        .stream()
        .anyMatch(entry -> entry.getKey().endsWith(".time") && entry.getValue() == gauge))
    .mapToLong(Gauge::getValue)
    .sum();

System.out.println("Total GC collections: " + totalCollections);
System.out.println("Total GC time: " + totalGcTime + " ms");

Performance Analysis:

  • High collection counts may indicate frequent minor GCs due to small heap or allocation rate
  • High collection times may indicate GC tuning issues or insufficient heap size
  • Ratio of time to count gives average GC duration per collection
  • Old generation collections are typically more expensive than young generation collections

Common GC Patterns:

  • Serial GC: Copy, MarkSweepCompact
  • Parallel GC: PS-Scavenge, PS-MarkSweep
  • CMS: ParNew, ConcurrentMarkSweep
  • G1: G1-Young-Generation, G1-Old-Generation
  • ZGC/Shenandoah: ZGC, Shenandoah

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