16
16
17
17
package org .springframework .boot .info ;
18
18
19
+ import java .lang .management .GarbageCollectorMXBean ;
19
20
import java .lang .management .ManagementFactory ;
20
21
import java .lang .management .MemoryMXBean ;
21
22
import java .lang .management .MemoryUsage ;
22
23
import java .lang .management .PlatformManagedObject ;
23
24
import java .lang .reflect .Method ;
25
+ import java .util .List ;
24
26
25
27
import org .springframework .util .ClassUtils ;
26
28
@@ -40,6 +42,9 @@ public class ProcessInfo {
40
42
41
43
private static final Runtime runtime = Runtime .getRuntime ();
42
44
45
+ private static final List <GarbageCollectorMXBean > garbageCollectorMXBeans = ManagementFactory
46
+ .getGarbageCollectorMXBeans ();
47
+
43
48
private final long pid ;
44
49
45
50
private final long parentPid ;
@@ -117,6 +122,19 @@ private <T> T invokeMethod(Class<?> mxbeanClass, Object mxbean, String name) thr
117
122
return (T ) method .invoke (mxbean );
118
123
}
119
124
125
+ /**
126
+ * Garbage Collector information for the process. This list provides details about the
127
+ * currently used GC algorithms selected by the user or JVM ergonomics. It might not
128
+ * be trivial to know the used GC algorithms since that usually depends on the
129
+ * {@link Runtime#availableProcessors()} (see: {@link ProcessInfo#getCpus()}) and the
130
+ * available memory (see: {@link ProcessInfo#getMemory()}).
131
+ * @return {@link List} of {@link GarbageCollectorInfo}.
132
+ * @since 3.5.0
133
+ */
134
+ public List <GarbageCollectorInfo > getGarbageCollectors () {
135
+ return garbageCollectorMXBeans .stream ().map (GarbageCollectorInfo ::new ).toList ();
136
+ }
137
+
120
138
public long getPid () {
121
139
return this .pid ;
122
140
}
@@ -223,4 +241,30 @@ public long getMax() {
223
241
224
242
}
225
243
244
+ /**
245
+ * Garbage Collector information.
246
+ *
247
+ * @since 3.5.0
248
+ */
249
+ public static class GarbageCollectorInfo {
250
+
251
+ private final String name ;
252
+
253
+ private final long collectionCount ;
254
+
255
+ GarbageCollectorInfo (GarbageCollectorMXBean garbageCollectorMXBean ) {
256
+ this .name = garbageCollectorMXBean .getName ();
257
+ this .collectionCount = garbageCollectorMXBean .getCollectionCount ();
258
+ }
259
+
260
+ public String getName () {
261
+ return this .name ;
262
+ }
263
+
264
+ public long getCollectionCount () {
265
+ return this .collectionCount ;
266
+ }
267
+
268
+ }
269
+
226
270
}
0 commit comments