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
@@ -40,6 +42,9 @@ public class ProcessInfo {
4042
4143 private static final Runtime runtime = Runtime .getRuntime ();
4244
45+ private static final List <GarbageCollectorMXBean > garbageCollectorMXBeans = ManagementFactory
46+ .getGarbageCollectorMXBeans ();
47+
4348 private final long pid ;
4449
4550 private final long parentPid ;
@@ -117,6 +122,19 @@ private <T> T invokeMethod(Class<?> mxbeanClass, Object mxbean, String name) thr
117122 return (T ) method .invoke (mxbean );
118123 }
119124
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+
120138 public long getPid () {
121139 return this .pid ;
122140 }
@@ -223,4 +241,30 @@ public long getMax() {
223241
224242 }
225243
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+
226270}
0 commit comments