0
# Class Loading Monitoring
1
2
Class loading statistics including total loaded and unloaded class counts, providing insight into application lifecycle and potential class loading issues.
3
4
## Core Imports
5
6
```java
7
import com.codahale.metrics.jvm.ClassLoadingGaugeSet;
8
import com.codahale.metrics.MetricRegistry;
9
import java.lang.management.ClassLoadingMXBean;
10
```
11
12
## Capabilities
13
14
### ClassLoadingGaugeSet
15
16
Monitors JVM class loading behavior through ClassLoadingMXBean integration.
17
18
```java { .api }
19
/**
20
* A set of gauges for JVM classloader usage.
21
*/
22
public class ClassLoadingGaugeSet implements MetricSet {
23
/**
24
* Creates a new set of class loading gauges using the default ClassLoadingMXBean.
25
*/
26
public ClassLoadingGaugeSet();
27
28
/**
29
* Creates a new set of class loading gauges using a custom ClassLoadingMXBean.
30
* @param mxBean the ClassLoadingMXBean to use for statistics
31
*/
32
public ClassLoadingGaugeSet(ClassLoadingMXBean mxBean);
33
34
/**
35
* Returns all class loading 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
- `loaded` - Total number of classes loaded since JVM start (cumulative count)
45
- `unloaded` - Total number of classes unloaded since JVM start (cumulative count)
46
47
**Usage Examples:**
48
49
```java
50
import com.codahale.metrics.MetricRegistry;
51
import com.codahale.metrics.jvm.ClassLoadingGaugeSet;
52
import java.lang.management.ManagementFactory;
53
54
// Basic usage with default ClassLoadingMXBean
55
MetricRegistry registry = new MetricRegistry();
56
registry.registerAll(new ClassLoadingGaugeSet());
57
58
// Access class loading metrics
59
Gauge<Long> totalLoaded = registry.getGauges().get("loaded");
60
Gauge<Long> totalUnloaded = registry.getGauges().get("unloaded");
61
62
System.out.println("Total classes loaded: " + totalLoaded.getValue());
63
System.out.println("Total classes unloaded: " + totalUnloaded.getValue());
64
65
// Calculate current loaded classes (approximation)
66
long currentlyLoaded = totalLoaded.getValue() - totalUnloaded.getValue();
67
System.out.println("Currently loaded classes: " + currentlyLoaded);
68
69
// Custom ClassLoadingMXBean usage
70
ClassLoadingMXBean customBean = ManagementFactory.getClassLoadingMXBean();
71
registry.registerAll(new ClassLoadingGaugeSet(customBean));
72
73
// Monitor class loading over time
74
long previousLoaded = totalLoaded.getValue();
75
long previousUnloaded = totalUnloaded.getValue();
76
77
// ... wait some time ...
78
79
long newLoaded = totalLoaded.getValue();
80
long newUnloaded = totalUnloaded.getValue();
81
82
long classesLoadedSinceLast = newLoaded - previousLoaded;
83
long classesUnloadedSinceLast = newUnloaded - previousUnloaded;
84
85
System.out.println("Classes loaded in period: " + classesLoadedSinceLast);
86
System.out.println("Classes unloaded in period: " + classesUnloadedSinceLast);
87
```
88
89
**Monitoring Patterns:**
90
91
- **Steady Growth in Loaded Classes**: Normal during application startup and dynamic loading
92
- **High Unload Activity**: May indicate frequent class reloading or memory pressure
93
- **No Unloading**: Common in production applications with static class sets
94
- **Rapid Loading/Unloading Cycles**: Possible indication of dynamic code generation or classloader leaks
95
96
**Performance Implications:**
97
98
- **Class loading** can be expensive, especially with complex dependency graphs
99
- **Class unloading** typically occurs during full GC when classes are no longer referenced
100
- **Monitoring ratios** of loaded to unloaded classes helps identify potential memory leaks
101
- **Sudden spikes** in class loading may correlate with application events or deployments
102
103
**Common Use Cases:**
104
105
- **Application Server Monitoring**: Track deployments and redeployments
106
- **Dynamic Framework Monitoring**: Monitor Spring, OSGi, or other dynamic loading frameworks
107
- **Memory Leak Detection**: Identify classloader leaks through increasing loaded class counts
108
- **Performance Analysis**: Correlate class loading with application startup times