I'm a bit confused on some of the results I am seeing from ps and free.
On my server, this is the result of free -m
[root@server ~]# free -m
total used free shared buffers cached
Mem: 2048 2033 14 0 73 1398
-/+ buffers/cache: 561 1486
Swap: 2047 11 2036
My understanding of how Linux manages memory, is that it will store disk usage in RAM, so that each subsequent access is quicker. I believe this is indicated by the "cached" columns. Additionally, various buffers are stored in RAM, indicated in the "buffers" column.
So if I understand correctly, the "actual" usage is supposed to be the "used" value of "-/+ buffers/cache", or 561 in this case.
So assuming all of that is correct, the part that throws me is the results of ps aux
.
My understanding of the ps
results, is that the 6th column (RSS), represents the size in kilobytes the process uses for memory.
So when I run this command:
[root@server ~]# ps aux | awk '{sum+=$6} END {print sum / 1024}'
1475.52
Shouldn't the result be the "used" column of "-/+ buffers/cache" from free -m
?
So, how can I properly determine the memory usage of a process in Linux? Apparently my logic is flawed.
Best Answer
Shamelessly copy/pasting my answer from serverfault just the other day :-)
The linux virtual memory system isn't quite so simple. You can't just add up all the RSS fields and get the value reported
used
byfree
. There are many reasons for this, but I'll hit a couple of the biggest ones.When a process forks, both the parent and the child will show with the same RSS. However linux employs copy-on-write so that both processes are really using the same memory. Only when one of the processes modifies the memory will it actually be duplicated.
This will cause the
free
number to be smaller than thetop
RSS sum.The RSS value doesn't include shared memory. Because shared memory isn't owned by any one process,
top
doesn't include it in RSS.This will cause the
free
number to be larger than thetop
RSS sum.