Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

jstorm 获取系统(linux)获取可用内存应该使用/proc/meminfo中的MemAvailable #427

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

superwood
Copy link

3.14 版本后 在/proc/meminfo文件中新增了一个选项

MemTotal: 7662552 kB
MemFree: 602996 kB
MemAvailable: 6005852 kB

应该只用MemAvailable去计算系统的free内存比较合理。

MemAvailable的意义如下:
Many load balancing and workload placing programs check /proc/meminfo to estimate how much free memory is available. They generally do this by adding up "free" and "cached", which was fine ten years ago, but is pretty much guaranteed to be wrong today.
It is wrong because Cached includes memory that is not freeable as page cache, for example shared memory segments, tmpfs, and ramfs, and it does not include reclaimable slab memory, which can take up a large fraction of system memory on mostly idle systems with lots of files.Currently, the amount of memory that is available for a new workload,without pushing the system into swap, can be estimated from MemFree, Active(file), Inactive(file), and SReclaimable, as well as the "low"watermarks from /proc/zoneinfo.However, this may change in the future, and user space really should not be expected to know kernel internals to come up with an estimate for the amount of free memory.It is more convenient to provide such an estimate in /proc/meminfo. If things change in the future, we only have to change it in one place

@CLAassistant
Copy link

CLAassistant commented Feb 9, 2017

CLA assistant check
All committers have signed the CLA.

String free = lines.get(2).split("\\s+")[1];
return Long.valueOf(free);
} catch (Exception ignored) {
LOG.warn("failed to get total free memory.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@superwood , 切换到available memory 没有什么问题,

但这里有个兼容性的问题, 如果os 不带“MemAvailable”时,需要额外增加一个逻辑,回退到getFreeMemory

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@longdafeng 没问题,可以在代码里面做一个兼容。 稍后重新提交下。

@superwood
Copy link
Author

@longdafeng 另外即使是回退到老的版本使用 MemFree: 602996 kB 去计算空闲内存也是有问题的。

理论上用 MemFree + Cached -Shmem 更加准确点。


List<String> lines = IOUtils.readLines(new FileInputStream(PROCFS_MEMINFO));
String free = lines.get(2).split("\\s+")[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的逻辑,我觉得应该反过来,默认使用mem free,当存在MemAvailable的时候才用这个。
另外,默认的情况下,需要考虑mem free + cache

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants