[procps] [PATCH] library: handle memory used by tmpfs

  • From: Jakob Unterwurzacher <jakobunt@xxxxxxxxx>
  • To: procps@xxxxxxxxxxxxx
  • Date: Thu, 16 Jan 2014 00:34:24 +0100

tmpfs has become much more widely used since distributions use it for
/tmp (Fedora 18+). This means we should probably handle its memory
usage properly (whatever that means).

For some reason, the kernel accounts tmpfs memory into both the page
cache and shared memory (
http://lxr.free-electrons.com/source/mm/shmem.c#L301 , at least since
kernel 3.0 ). The tools just pass it on, so what top and free report as
"cached" is actually the sum of page cache and tmpfs.

free also has the extremely useful "-/+ buffers/cache" output. However,
now that tmpfs is accounted into "cached", those numbers are way off
once you have big files in /tmp. If you write, say, 1GB into /tmp, the
numbers do not change at all, while you actually have 1GB less usable
memory. You can actually be OOM while free reports lots of free memory.

The kernel exports the memory used (in RAM) by tmpfs as "Shmem" in
/proc/meminfo. This patch substracts Shmem from Cached to get the actual
page cache figure. This makes both issues disappear.
---
 proc/sysinfo.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/proc/sysinfo.c b/proc/sysinfo.c
index f318376..b8328fd 100644
--- a/proc/sysinfo.c
+++ b/proc/sysinfo.c
@@ -684,6 +684,8 @@ nextline:
   }
   kb_swap_used = kb_swap_total - kb_swap_free;
   kb_main_used = kb_main_total - kb_main_free;
+  /* "Cached" includes "Shmem", we want only the page cache here */
+  kb_main_cached -= kb_main_shared;
 }

 /*****************************************************************/
-- 
1.8.4.2

Other related posts: