1616
1717package org .springframework .boot .info ;
1818
19+ import java .lang .management .GarbageCollectorMXBean ;
1920import java .lang .management .ManagementFactory ;
2021import java .lang .management .MemoryMXBean ;
2122import java .lang .management .MemoryUsage ;
2223import java .lang .management .PlatformManagedObject ;
2324import java .lang .reflect .Method ;
25+ import java .util .List ;
2426
2527import org .springframework .util .ClassUtils ;
2628
@@ -178,13 +180,19 @@ public static class MemoryInfo {
178180
179181 private static final MemoryMXBean memoryMXBean = ManagementFactory .getMemoryMXBean ();
180182
183+ private static final List <GarbageCollectorMXBean > garbageCollectorMXBeans = ManagementFactory
184+ .getGarbageCollectorMXBeans ();
185+
181186 private final MemoryUsageInfo heap ;
182187
183188 private final MemoryUsageInfo nonHeap ;
184189
190+ private final List <GarbageCollectorInfo > garbageCollectors ;
191+
185192 MemoryInfo () {
186193 this .heap = new MemoryUsageInfo (memoryMXBean .getHeapMemoryUsage ());
187194 this .nonHeap = new MemoryUsageInfo (memoryMXBean .getNonHeapMemoryUsage ());
195+ this .garbageCollectors = garbageCollectorMXBeans .stream ().map (GarbageCollectorInfo ::new ).toList ();
188196 }
189197
190198 public MemoryUsageInfo getHeap () {
@@ -195,6 +203,20 @@ public MemoryUsageInfo getNonHeap() {
195203 return this .nonHeap ;
196204 }
197205
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+
198220 public static class MemoryUsageInfo {
199221
200222 private final MemoryUsage memoryUsage ;
@@ -221,6 +243,27 @@ public long getMax() {
221243
222244 }
223245
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+
224267 }
225268
226269}
0 commit comments