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
@@ -178,13 +180,19 @@ public static class MemoryInfo {
178
180
179
181
private static final MemoryMXBean memoryMXBean = ManagementFactory .getMemoryMXBean ();
180
182
183
+ private static final List <GarbageCollectorMXBean > garbageCollectorMXBeans = ManagementFactory
184
+ .getGarbageCollectorMXBeans ();
185
+
181
186
private final MemoryUsageInfo heap ;
182
187
183
188
private final MemoryUsageInfo nonHeap ;
184
189
190
+ private final List <GarbageCollectorInfo > garbageCollectors ;
191
+
185
192
MemoryInfo () {
186
193
this .heap = new MemoryUsageInfo (memoryMXBean .getHeapMemoryUsage ());
187
194
this .nonHeap = new MemoryUsageInfo (memoryMXBean .getNonHeapMemoryUsage ());
195
+ this .garbageCollectors = garbageCollectorMXBeans .stream ().map (GarbageCollectorInfo ::new ).toList ();
188
196
}
189
197
190
198
public MemoryUsageInfo getHeap () {
@@ -195,6 +203,20 @@ public MemoryUsageInfo getNonHeap() {
195
203
return this .nonHeap ;
196
204
}
197
205
206
+ /**
207
+ * Garbage Collector information for the process. This list provides details about
208
+ * the currently used GC algorithms selected by the user or JVM ergonomics. It
209
+ * might not be trivial to know the used GC algorithms since that usually depends
210
+ * on the {@link Runtime#availableProcessors()} (see:
211
+ * {@link ProcessInfo#getCpus()}) and the available memory (see:
212
+ * {@link MemoryUsageInfo}).
213
+ * @return {@link List} of {@link GarbageCollectorInfo}.
214
+ * @since 3.5.0
215
+ */
216
+ public List <GarbageCollectorInfo > getGarbageCollectors () {
217
+ return this .garbageCollectors ;
218
+ }
219
+
198
220
public static class MemoryUsageInfo {
199
221
200
222
private final MemoryUsage memoryUsage ;
@@ -221,6 +243,27 @@ public long getMax() {
221
243
222
244
}
223
245
246
+ public static class GarbageCollectorInfo {
247
+
248
+ private final String name ;
249
+
250
+ private final long collectionCount ;
251
+
252
+ GarbageCollectorInfo (GarbageCollectorMXBean garbageCollectorMXBean ) {
253
+ this .name = garbageCollectorMXBean .getName ();
254
+ this .collectionCount = garbageCollectorMXBean .getCollectionCount ();
255
+ }
256
+
257
+ public String getName () {
258
+ return this .name ;
259
+ }
260
+
261
+ public long getCollectionCount () {
262
+ return this .collectionCount ;
263
+ }
264
+
265
+ }
266
+
224
267
}
225
268
226
269
}
0 commit comments