Poison

OperatingSystemMXBean

OperatingSystemMXBean 中的 getFreePhysicalMemorySize() 方法返回的值为不含缓存的可用物理内存,该指标不能很好的反应 Linux 中实际可用的物理内存,因为 Linux 尽量使用内存的特性,该指标返回的数据几乎总是接近总物理内存,我们根据 /proc/meminfo 中返回的数据采集出类似 free 命令中 available 列的值以便更好的监测可用物理内存。

部分代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Metrics collection similar to the free command.
* <p>See also the <a href='https://man7.org/linux/man-pages/man1/free.1.html'>free(1) — Linux manual page</a>.
*
*/
public interface OperatingSystemMemoryMXBean {

/**
* Returns the amount of used physical memory in bytes.
* Used memory (calculated as MemTotal - MemAvailable in /proc/meminfo)
*
* @return the amount of used physical memory in bytes.
*/
long getUsedPhysicalMemorySize();

/**
* Returns the total amount of physical memory in bytes.
* Total memory (MemTotal in /proc/meminfo)
*
* @return the total amount of physical memory in bytes.
*/
long getTotalPhysicalMemorySize();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class OperatingSystemMemoryImpl implements OperatingSystemMemoryMXBean {

private static final String METRICS_MEM_TOTAL = "MemTotal";
private static final String METRICS_MEM_AVAILABLE = "MemAvailable";

@Override
public long getUsedPhysicalMemorySize() {
Map<String, Long> metricsToValueMap = getMetricsToValueMapFromMemInfo();
return metricsToValueMap.getOrDefault(METRICS_MEM_TOTAL, 0L) - metricsToValueMap.getOrDefault(METRICS_MEM_AVAILABLE, 0L);
}

@Override
public long getTotalPhysicalMemorySize() {
Map<String, Long> metricsToValueMap = getMetricsToValueMapFromMemInfo();
return metricsToValueMap.getOrDefault(METRICS_MEM_TOTAL, 0L);
}

private Map<String, Long> getMetricsToValueMapFromMemInfo() {
Map<String, Long> metricsToValueMap = new HashMap<>(); // value unit: byte

try (BufferedReader reader = new BufferedReader(new FileReader("/proc/meminfo"))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.endsWith("kB")) {
continue;
}

int colonIndex = line.indexOf(':');
int lastSpaceIndex = line.lastIndexOf(' ');
if (colonIndex != -1 && lastSpaceIndex != -1) {
String metrics = line.substring(0, colonIndex);
int firstDigitIndex = colonIndex;
while (!Character.isDigit(line.charAt(firstDigitIndex))) {
firstDigitIndex++;
}
long valueInKB = Long.parseLong(line.substring(firstDigitIndex, lastSpaceIndex));

metricsToValueMap.put(metrics, valueInKB * 1024);
}
}

return metricsToValueMap;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Reference

How to monitor free memory ( including buffers and cache) in java? - Stack Overflow
jdk/OperatingSystemMXBean.java at jdk8-b120 · openjdk/jdk · GitHub
free(1) - Linux manual page
proc(5) - Linux manual page
extras-memory-monitor/ProcMemInfo.java at master · jenkinsci/extras-memory-monitor · GitHub
How to calculate system memory usage from /proc/meminfo (like htop) - Stack Overflow
/proc/meminfo: provide estimated available memory - Linux kernel source tree