JVM Integration for Metrics - A set of classes which allow you to monitor critical aspects of your Java Virtual Machine using Metrics
—
Garbage collection statistics including collection counts and elapsed times for all discoverable garbage collectors in the JVM.
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.MetricRegistry;
import java.lang.management.GarbageCollectorMXBean;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 collectorPS-MarkSweep.count / PS-MarkSweep.time - Parallel Mark Sweep old generation collectorG1-Young-Generation.count / G1-Young-Generation.time - G1 young generation collectorG1-Old-Generation.count / G1-Old-Generation.time - G1 old generation collectorConcurrentMarkSweep.count / ConcurrentMarkSweep.time - CMS collectorParNew.count / ParNew.time - Parallel New young generation collectorUsage 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:
Common GC Patterns:
Copy, MarkSweepCompactPS-Scavenge, PS-MarkSweepParNew, ConcurrentMarkSweepG1-Young-Generation, G1-Old-GenerationZGC, ShenandoahInstall with Tessl CLI
npx tessl i tessl/maven-io-dropwizard-metrics--metrics-jvm