midenok midenok - 1 month ago 7
Linux Question

Why there is big difference in memory usage between ps and free?

# ps hax -o rss|paste -d+ -s|bc; free
3963568
total used free shared buffers cached
Mem: 8176380 7602512 573868 119048 680236 501084
-/+ buffers/cache: 6421192 1755188
Swap: 4199420 327820 3871600


Sum of RSS from
ps
shows
3963568
while
free
tells
6421192
. What else goes to
free
and how can I track it with
ps
or other tools? Is it fragmentation?

Answer

As noted at How to measure actual memory usage of an application or process?:

Why ps is "wrong"

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely "wrong".

So your ps command will tend to OVERCOUNT the memory used by processes, as it double-counts memory that is shared.

On the other hand, I can see you are looking at the correct line of the free output which discards the memory used for buffers/cache. You need to look in /proc/meminfo to see what is using up most of the discrepancy in RAM, as discussed at http://serverfault.com/questions/240277/slab-uses-88gb-of-128gb-available-what-could-cause-this.

Slab cache is separate from the buffers/cache reported by free, so assuming it is responsible for most of the discrepancy you can see what it is used for in /proc/slabinfo. If it's dentries (dentry_cache line) or inodes (there are many *inode_cache lines), you can use the following to free up the RAM:

sync; echo 2 >/proc/sys/vm/drop_caches 

to get rid of them.

Comments