1616
1717package org .springframework .boot .info ;
1818
19+ import java .lang .management .ManagementFactory ;
20+ import java .lang .management .MemoryMXBean ;
21+ import java .lang .management .MemoryUsage ;
22+
1923/**
2024 * Information about the process of the application.
2125 *
@@ -50,6 +54,24 @@ public int getCpus() {
5054 return runtime .availableProcessors ();
5155 }
5256
57+ /**
58+ * Memory information for the process. These values can provide details about the
59+ * current memory usage and limits selected by the user or JVM ergonomics (init, max,
60+ * committed, used for heap and non-heap). If limits not set explicitly, it might not
61+ * be trivial to know what these values are runtime; especially in (containerized)
62+ * environments where resource usage can be isolated (for example using control
63+ * groups) or not necessarily trivial to discover. Other than that, these values can
64+ * indicate if the JVM can resize the heap (stop-the-world).
65+ * @return heap and non-heap memory information
66+ * @since 3.4.0
67+ * @see MemoryMXBean#getHeapMemoryUsage()
68+ * @see MemoryMXBean#getNonHeapMemoryUsage()
69+ * @see MemoryUsage
70+ */
71+ public MemoryInfo getMemory () {
72+ return new MemoryInfo ();
73+ }
74+
5375 public long getPid () {
5476 return this .pid ;
5577 }
@@ -62,4 +84,58 @@ public String getOwner() {
6284 return this .owner ;
6385 }
6486
87+ /**
88+ * Memory information.
89+ *
90+ * @since 3.4.0
91+ */
92+ public static class MemoryInfo {
93+
94+ private static final MemoryMXBean memoryMXBean = ManagementFactory .getMemoryMXBean ();
95+
96+ private final MemoryUsageInfo heap ;
97+
98+ private final MemoryUsageInfo nonHeap ;
99+
100+ MemoryInfo () {
101+ this .heap = new MemoryUsageInfo (memoryMXBean .getHeapMemoryUsage ());
102+ this .nonHeap = new MemoryUsageInfo (memoryMXBean .getNonHeapMemoryUsage ());
103+ }
104+
105+ public MemoryUsageInfo getHeap () {
106+ return this .heap ;
107+ }
108+
109+ public MemoryUsageInfo getNonHeap () {
110+ return this .nonHeap ;
111+ }
112+
113+ public static class MemoryUsageInfo {
114+
115+ private final MemoryUsage memoryUsage ;
116+
117+ MemoryUsageInfo (MemoryUsage memoryUsage ) {
118+ this .memoryUsage = memoryUsage ;
119+ }
120+
121+ public long getInit () {
122+ return this .memoryUsage .getInit ();
123+ }
124+
125+ public long getUsed () {
126+ return this .memoryUsage .getUsed ();
127+ }
128+
129+ public long getCommited () {
130+ return this .memoryUsage .getCommitted ();
131+ }
132+
133+ public long getMax () {
134+ return this .memoryUsage .getMax ();
135+ }
136+
137+ }
138+
139+ }
140+
65141}
0 commit comments