Skip to content

Commit fe7d621

Browse files
committed
memory: Avoid estimating available memory, use kernel estimate instead
`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
1 parent 28c263c commit fe7d621

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/blocks/memory.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,9 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
145145

146146
let buffers = mem_state.buffers as f64;
147147

148-
// same logic as htop
149-
let used_diff = mem_free + buffers + pagecache + reclaimable;
150-
let mem_used = if mem_total >= used_diff {
151-
mem_total - used_diff
152-
} else {
153-
mem_total - mem_free
154-
};
148+
// Userspace should use `mem_avail` for estimating the memory that is available.
149+
// See: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
150+
let mem_used = mem_total - mem_avail;
155151

156152
// account for ZFS ARC cache
157153
let mem_used = mem_used - zfs_shrinkable_size;

0 commit comments

Comments
 (0)