0
# Garbage Collection Monitoring
1
2
Garbage collection statistics including collection counts and elapsed times for all discoverable garbage collectors in the JVM.
3
4
## Core Imports
5
6
```java
7
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
8
import com.codahale.metrics.MetricRegistry;
9
import java.lang.management.GarbageCollectorMXBean;
10
```
11
12
## Capabilities
13
14
### GarbageCollectorMetricSet
15
16
Provides comprehensive garbage collection metrics for monitoring GC performance and behavior.
17
18
```java { .api }
19
/**
20
* A set of gauges for the counts and elapsed times of garbage collections.
21
*/
22
public class GarbageCollectorMetricSet implements MetricSet {
23
/**
24
* Creates a new set of gauges for all discoverable garbage collectors.
25
*/
26
public GarbageCollectorMetricSet();
27
28
/**
29
* Creates a new set of gauges for the given collection of garbage collectors.
30
* @param garbageCollectors the garbage collectors to monitor
31
*/
32
public GarbageCollectorMetricSet(Collection<GarbageCollectorMXBean> garbageCollectors);
33
34
/**
35
* Returns all garbage collection metrics.
36
* @return map of metric names to Metric instances
37
*/
38
public Map<String, Metric> getMetrics();
39
}
40
```
41
42
**Metrics Provided:**
43
44
For each garbage collector, two metrics are provided:
45
- `{gc-name}.count` - Total number of collections performed
46
- `{gc-name}.time` - Total time spent in collections (milliseconds)
47
48
Common garbage collector names (whitespace normalized to hyphens):
49
- `PS-Scavenge.count` / `PS-Scavenge.time` - Parallel Scavenge young generation collector
50
- `PS-MarkSweep.count` / `PS-MarkSweep.time` - Parallel Mark Sweep old generation collector
51
- `G1-Young-Generation.count` / `G1-Young-Generation.time` - G1 young generation collector
52
- `G1-Old-Generation.count` / `G1-Old-Generation.time` - G1 old generation collector
53
- `ConcurrentMarkSweep.count` / `ConcurrentMarkSweep.time` - CMS collector
54
- `ParNew.count` / `ParNew.time` - Parallel New young generation collector
55
56
**Usage Examples:**
57
58
```java
59
import com.codahale.metrics.MetricRegistry;
60
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
61
import java.lang.management.ManagementFactory;
62
63
// Basic usage with all discoverable GC beans
64
MetricRegistry registry = new MetricRegistry();
65
registry.registerAll(new GarbageCollectorMetricSet());
66
67
// Access GC metrics
68
Gauge<Long> youngGcCount = registry.getGauges().get("PS-Scavenge.count");
69
Gauge<Long> youngGcTime = registry.getGauges().get("PS-Scavenge.time");
70
Gauge<Long> oldGcCount = registry.getGauges().get("PS-MarkSweep.count");
71
Gauge<Long> oldGcTime = registry.getGauges().get("PS-MarkSweep.time");
72
73
if (youngGcCount != null) {
74
System.out.println("Young GC collections: " + youngGcCount.getValue());
75
System.out.println("Young GC time: " + youngGcTime.getValue() + " ms");
76
}
77
78
if (oldGcCount != null) {
79
System.out.println("Old GC collections: " + oldGcCount.getValue());
80
System.out.println("Old GC time: " + oldGcTime.getValue() + " ms");
81
}
82
83
// Custom GC bean collection
84
Collection<GarbageCollectorMXBean> specificCollectors = ManagementFactory.getGarbageCollectorMXBeans()
85
.stream()
86
.filter(gc -> gc.getName().contains("G1"))
87
.collect(Collectors.toList());
88
registry.registerAll(new GarbageCollectorMetricSet(specificCollectors));
89
90
// Calculate GC frequency and throughput
91
long totalCollections = registry.getGauges().values().stream()
92
.filter(gauge -> gauge instanceof Gauge)
93
.map(gauge -> (Gauge<Long>) gauge)
94
.filter(gauge -> registry.getGauges()
95
.entrySet()
96
.stream()
97
.anyMatch(entry -> entry.getKey().endsWith(".count") && entry.getValue() == gauge))
98
.mapToLong(Gauge::getValue)
99
.sum();
100
101
long totalGcTime = registry.getGauges().values().stream()
102
.filter(gauge -> gauge instanceof Gauge)
103
.map(gauge -> (Gauge<Long>) gauge)
104
.filter(gauge -> registry.getGauges()
105
.entrySet()
106
.stream()
107
.anyMatch(entry -> entry.getKey().endsWith(".time") && entry.getValue() == gauge))
108
.mapToLong(Gauge::getValue)
109
.sum();
110
111
System.out.println("Total GC collections: " + totalCollections);
112
System.out.println("Total GC time: " + totalGcTime + " ms");
113
```
114
115
**Performance Analysis:**
116
117
- **High collection counts** may indicate frequent minor GCs due to small heap or allocation rate
118
- **High collection times** may indicate GC tuning issues or insufficient heap size
119
- **Ratio of time to count** gives average GC duration per collection
120
- **Old generation collections** are typically more expensive than young generation collections
121
122
**Common GC Patterns:**
123
124
- **Serial GC**: `Copy`, `MarkSweepCompact`
125
- **Parallel GC**: `PS-Scavenge`, `PS-MarkSweep`
126
- **CMS**: `ParNew`, `ConcurrentMarkSweep`
127
- **G1**: `G1-Young-Generation`, `G1-Old-Generation`
128
- **ZGC/Shenandoah**: `ZGC`, `Shenandoah`