Skip to content

Commit a9fce43

Browse files
Add MemoryInfo to ProcessInfo
1 parent 895fbd7 commit a9fce43

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/ProcessInfo.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package 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,53 @@ public String getOwner() {
6284
return this.owner;
6385
}
6486

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+
65136
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/info/ProcessInfoTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.boot.info.ProcessInfo.MemoryInfo.MemoryUsageInfo;
22+
2123
import static org.assertj.core.api.Assertions.assertThat;
2224

2325
/**
@@ -35,6 +37,17 @@ void processInfoIsAvailable() {
3537
assertThat(processInfo.getPid()).isEqualTo(ProcessHandle.current().pid());
3638
assertThat(processInfo.getParentPid())
3739
.isEqualTo(ProcessHandle.current().parent().map(ProcessHandle::pid).orElse(null));
40+
41+
MemoryUsageInfo heapUsageInfo = processInfo.getMemory().getHeap();
42+
MemoryUsageInfo nonHeapUsageInfo = processInfo.getMemory().getNonHeap();
43+
assertThat(heapUsageInfo.getInit()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getMax());
44+
assertThat(heapUsageInfo.getUsed()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getCommited());
45+
assertThat(heapUsageInfo.getCommited()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getMax());
46+
assertThat(heapUsageInfo.getMax()).isPositive();
47+
assertThat(nonHeapUsageInfo.getInit()).isPositive();
48+
assertThat(nonHeapUsageInfo.getUsed()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getCommited());
49+
assertThat(nonHeapUsageInfo.getCommited()).isPositive();
50+
assertThat(nonHeapUsageInfo.getMax()).isEqualTo(-1);
3851
}
3952

4053
}

0 commit comments

Comments
 (0)