JVM Integration for Metrics - A set of classes which allow you to monitor critical aspects of your Java Virtual Machine using Metrics
npx @tessl/cli install tessl/maven-io-dropwizard-metrics--metrics-jvm@4.2.00
# Metrics JVM
1
2
Metrics JVM provides comprehensive Java Virtual Machine monitoring capabilities through the Dropwizard Metrics library. It enables real-time monitoring of critical JVM aspects including memory usage, garbage collection, thread states, class loading, buffer pools, and system attributes through JMX integration with minimal performance overhead.
3
4
## Package Information
5
6
- **Package Name**: io.dropwizard.metrics:metrics-jvm
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Installation**:
10
```xml
11
<dependency>
12
<groupId>io.dropwizard.metrics</groupId>
13
<artifactId>metrics-jvm</artifactId>
14
<version>4.2.33</version>
15
</dependency>
16
```
17
18
## Core Imports
19
20
```java
21
import com.codahale.metrics.jvm.*;
22
import com.codahale.metrics.MetricRegistry;
23
```
24
25
## Basic Usage
26
27
```java
28
import com.codahale.metrics.MetricRegistry;
29
import com.codahale.metrics.jvm.*;
30
31
// Create a metric registry
32
MetricRegistry registry = new MetricRegistry();
33
34
// Register JVM metrics
35
registry.registerAll(new MemoryUsageGaugeSet());
36
registry.registerAll(new GarbageCollectorMetricSet());
37
registry.registerAll(new ThreadStatesGaugeSet());
38
registry.registerAll(new ClassLoadingGaugeSet());
39
registry.registerAll(new JvmAttributeGaugeSet());
40
41
// For buffer pools (Java 7+)
42
registry.registerAll(new BufferPoolMetricSet(
43
ManagementFactory.getPlatformMBeanServer()));
44
45
// Get specific metrics
46
Gauge<Double> heapUsage = registry.getGauges().get("heap.usage");
47
System.out.println("Heap usage: " + heapUsage.getValue());
48
```
49
50
## Architecture
51
52
Metrics JVM is organized around several key components:
53
54
- **MetricSet Implementations**: Provide collections of related metrics that can be registered with a MetricRegistry
55
- **Gauge Implementations**: Individual metric providers for specific measurements
56
- **Utility Classes**: Helper classes for thread dump generation and deadlock detection
57
- **JMX Integration**: Direct integration with Java Management Extensions for real-time monitoring
58
- **Caching Support**: Optional caching for performance optimization in high-frequency monitoring scenarios
59
60
## Capabilities
61
62
### Memory Monitoring
63
64
Comprehensive memory usage tracking including heap, non-heap, and individual memory pool statistics with usage ratios and garbage collection metrics.
65
66
```java { .api }
67
public class MemoryUsageGaugeSet implements MetricSet {
68
public MemoryUsageGaugeSet();
69
public MemoryUsageGaugeSet(MemoryMXBean mxBean, Collection<MemoryPoolMXBean> memoryPools);
70
public Map<String, Metric> getMetrics();
71
}
72
```
73
74
[Memory Monitoring](./memory-monitoring.md)
75
76
### Thread State Monitoring
77
78
Real-time thread state tracking, deadlock detection, and thread dump generation with optional caching for performance optimization.
79
80
```java { .api }
81
public class ThreadStatesGaugeSet implements MetricSet {
82
public ThreadStatesGaugeSet();
83
public ThreadStatesGaugeSet(ThreadMXBean threads, ThreadDeadlockDetector deadlockDetector);
84
public Map<String, Metric> getMetrics();
85
}
86
87
public class CachedThreadStatesGaugeSet extends ThreadStatesGaugeSet {
88
public CachedThreadStatesGaugeSet(long interval, TimeUnit unit);
89
public CachedThreadStatesGaugeSet(ThreadMXBean threadMXBean, ThreadDeadlockDetector deadlockDetector, long interval, TimeUnit unit);
90
}
91
```
92
93
[Thread Monitoring](./thread-monitoring.md)
94
95
### Garbage Collection Monitoring
96
97
Garbage collection statistics including collection counts and elapsed times for all discoverable garbage collectors.
98
99
```java { .api }
100
public class GarbageCollectorMetricSet implements MetricSet {
101
public GarbageCollectorMetricSet();
102
public GarbageCollectorMetricSet(Collection<GarbageCollectorMXBean> garbageCollectors);
103
public Map<String, Metric> getMetrics();
104
}
105
```
106
107
[Garbage Collection Monitoring](./gc-monitoring.md)
108
109
### Buffer Pool Monitoring
110
111
Buffer pool usage statistics for direct and mapped buffers, available on Java 7 and above.
112
113
```java { .api }
114
public class BufferPoolMetricSet implements MetricSet {
115
public BufferPoolMetricSet(MBeanServer mBeanServer);
116
public Map<String, Metric> getMetrics();
117
}
118
```
119
120
[Buffer Pool Monitoring](./buffer-pool-monitoring.md)
121
122
### Class Loading Monitoring
123
124
Class loading statistics including total loaded and unloaded class counts.
125
126
```java { .api }
127
public class ClassLoadingGaugeSet implements MetricSet {
128
public ClassLoadingGaugeSet();
129
public ClassLoadingGaugeSet(ClassLoadingMXBean mxBean);
130
public Map<String, Metric> getMetrics();
131
}
132
```
133
134
[Class Loading Monitoring](./class-loading-monitoring.md)
135
136
### JVM Attributes
137
138
JVM runtime information including name, vendor details, and uptime tracking.
139
140
```java { .api }
141
public class JvmAttributeGaugeSet implements MetricSet {
142
public JvmAttributeGaugeSet();
143
public JvmAttributeGaugeSet(RuntimeMXBean runtime);
144
public Map<String, Metric> getMetrics();
145
}
146
```
147
148
[JVM Attributes](./jvm-attributes.md)
149
150
### File Descriptor Monitoring
151
152
File descriptor usage ratio monitoring for Unix-based systems.
153
154
```java { .api }
155
public class FileDescriptorRatioGauge extends RatioGauge {
156
public FileDescriptorRatioGauge();
157
public FileDescriptorRatioGauge(OperatingSystemMXBean os);
158
}
159
```
160
161
[File Descriptor Monitoring](./file-descriptor-monitoring.md)
162
163
### JMX Attribute Access
164
165
Generic JMX attribute monitoring for custom MBean attributes.
166
167
```java { .api }
168
public class JmxAttributeGauge implements Gauge<Object> {
169
public JmxAttributeGauge(ObjectName objectName, String attributeName);
170
public JmxAttributeGauge(MBeanServerConnection mBeanServerConn, ObjectName objectName, String attributeName);
171
public Object getValue();
172
}
173
```
174
175
[JMX Attribute Access](./jmx-attribute-access.md)
176
177
### CPU Time Monitoring
178
179
CPU time measurement capabilities for performance monitoring.
180
181
```java { .api }
182
public class CpuTimeClock extends Clock {
183
public long getTick();
184
}
185
```
186
187
[CPU Time Monitoring](./cpu-time-monitoring.md)
188
189
## Types
190
191
```java { .api }
192
// Core Metrics library types
193
import com.codahale.metrics.MetricSet;
194
import com.codahale.metrics.Gauge;
195
import com.codahale.metrics.RatioGauge;
196
import com.codahale.metrics.Clock;
197
import com.codahale.metrics.Metric;
198
199
// JMX types
200
import javax.management.MBeanServer;
201
import javax.management.MBeanServerConnection;
202
import javax.management.ObjectName;
203
204
// Java Management types
205
import java.lang.management.*;
206
import java.util.concurrent.TimeUnit;
207
```