Skip to content

memory: Avoid estimating available memory, use kernel estimate instead #2160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

bpeetz
Copy link

@bpeetz bpeetz commented May 19, 2025

free, btop and other tools (like the sysinfo rust library) use mem_total - mem_avail to calculate the used memory. As explained in the already linked kernel commit, mem_avail is an estimate of the available memory based on the current way the kernel handles memory.

The previous logic for calculating used memory tried to estimate this based on mem_free, buffers, pagecache and reclaimable. Unfortunately, as the kernel commit message predicts, this user space estimate bitrots, as the kernel memory usage patterns change.

Thus, we now simply use the kernel estimate mem_avail as we know that that is in-sync with the relevant kernel internals.

bpeetz added 2 commits May 19, 2025 21:26
`/proc/meminfo` states that it's report values are in KB, when
they actually are in KiB. Previously, this inconsistency leaked into the
whole code for this block (which had to add `* 1024` after nearly every
assignment). Now this inconsistency is contained to the `Memstate`
structure.
`free`, `btop` and other tools (like the `sysinfo` rust library)
use `mem_total - mem_avail` to calculate the used memory. As explained
in the already linked [kernel commit][1], `mem_avail` is an estimate of
the available memory based on the current way the kernel handles
memory.

The previous logic for calculating used memory tried to estimate
this based on `mem_free`, `buffers`, `pagecache` and
`reclaimable`. Unfortunately, as the kernel commit message predicts,
this user space estimate bitrots, as the kernel memory usage patterns change.

Thus, we now simply use the kernel estimate `mem_avail` as we know that
that is in-sync with the relevant kernel internals.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
@bpeetz bpeetz force-pushed the correct_memory_calc branch from 3b43b58 to fe7d621 Compare May 19, 2025 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant