Skip to content

Commit 5689bf5

Browse files
committed
Merge pull request #41262 from jonatan-ivanov
* pr/41262: Polish "Add MemoryInfo to ProcessInfo" Add MemoryInfo to ProcessInfo Closes gh-41262
2 parents 0fafb8d + cbce494 commit 5689bf5

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

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

Lines changed: 76 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,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
}

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

Lines changed: 17 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
/**
@@ -37,4 +39,19 @@ void processInfoIsAvailable() {
3739
.isEqualTo(ProcessHandle.current().parent().map(ProcessHandle::pid).orElse(null));
3840
}
3941

42+
@Test
43+
void memoryInfoIsAvailable() {
44+
ProcessInfo processInfo = new ProcessInfo();
45+
MemoryUsageInfo heapUsageInfo = processInfo.getMemory().getHeap();
46+
MemoryUsageInfo nonHeapUsageInfo = processInfo.getMemory().getNonHeap();
47+
assertThat(heapUsageInfo.getInit()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getMax());
48+
assertThat(heapUsageInfo.getUsed()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getCommited());
49+
assertThat(heapUsageInfo.getCommited()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getMax());
50+
assertThat(heapUsageInfo.getMax()).isPositive();
51+
assertThat(nonHeapUsageInfo.getInit()).isPositive();
52+
assertThat(nonHeapUsageInfo.getUsed()).isPositive().isLessThanOrEqualTo(heapUsageInfo.getCommited());
53+
assertThat(nonHeapUsageInfo.getCommited()).isPositive();
54+
assertThat(nonHeapUsageInfo.getMax()).isEqualTo(-1);
55+
}
56+
4057
}

0 commit comments

Comments
 (0)