16
16
17
17
package org .springframework .boot .info ;
18
18
19
+ import java .lang .management .ManagementFactory ;
20
+ import java .lang .management .MemoryMXBean ;
21
+ import java .lang .management .MemoryUsage ;
22
+
19
23
/**
20
24
* Information about the process of the application.
21
25
*
@@ -50,6 +54,24 @@ public int getCpus() {
50
54
return runtime .availableProcessors ();
51
55
}
52
56
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
+
53
75
public long getPid () {
54
76
return this .pid ;
55
77
}
@@ -62,4 +84,53 @@ public String getOwner() {
62
84
return this .owner ;
63
85
}
64
86
87
+ public static class MemoryInfo {
88
+
89
+ private static final MemoryMXBean memoryMXBean = ManagementFactory .getMemoryMXBean ();
90
+
91
+ private final MemoryUsageInfo heap ;
92
+
93
+ private final MemoryUsageInfo nonHeap ;
94
+
95
+ MemoryInfo () {
96
+ this .heap = new MemoryUsageInfo (memoryMXBean .getHeapMemoryUsage ());
97
+ this .nonHeap = new MemoryUsageInfo (memoryMXBean .getNonHeapMemoryUsage ());
98
+ }
99
+
100
+ public MemoryUsageInfo getHeap () {
101
+ return this .heap ;
102
+ }
103
+
104
+ public MemoryUsageInfo getNonHeap () {
105
+ return this .nonHeap ;
106
+ }
107
+
108
+ public static class MemoryUsageInfo {
109
+
110
+ private final MemoryUsage memoryUsage ;
111
+
112
+ MemoryUsageInfo (MemoryUsage memoryUsage ) {
113
+ this .memoryUsage = memoryUsage ;
114
+ }
115
+
116
+ public long getInit () {
117
+ return this .memoryUsage .getInit ();
118
+ }
119
+
120
+ public long getUsed () {
121
+ return this .memoryUsage .getUsed ();
122
+ }
123
+
124
+ public long getCommited () {
125
+ return this .memoryUsage .getCommitted ();
126
+ }
127
+
128
+ public long getMax () {
129
+ return this .memoryUsage .getMax ();
130
+ }
131
+
132
+ }
133
+
134
+ }
135
+
65
136
}
0 commit comments