Tools like 'ps' and 'top' report various kinds of memory usages, such as the VM size and the Resident Set Size. However, none of those are the "real" memory usage:
- Program code is shared between multiple instances of the same program.
- Shared library program code is shared between all processes that use that library.
- Some apps fork off processes and share memory with them (e.g. via shared memory segments).
- The virtual memory system makes the VM size report pretty much useless.
- RSS is 0 when a process is swapped out, making it not very useful.
- Etc etc.
I've found that the private dirty RSS, as reported by Linux, is the closest thing to the "real" memory usage. This can be obtained by summing all Private_Dirty
values in /proc/somepid/smaps
.
However, do other operating systems provide similar functionality? If not, what are the alternatives? In particular, I'm interested in FreeBSD and OS X.
You really can't.
I mean, shared memory between processes... are you going to count it, or not. If you don't count it, you are wrong; the sum of all processes' memory usage is not going to be the total memory usage. If you count it, you are going to count it twice- the sum's not going to be correct.
Me, I'm happy with RSS. And knowing you can't really rely on it completely...
On Linux, you may want the PSS (proportional set size) numbers in /proc/self/smaps. A mapping's PSS is its RSS divided by the number of processes which are using that mapping.
Check it out, this is the source code of gnome-system-monitor, it thinks the memory "really used" by one process is sum(
info->mem
) of X Server Memory(info->memxserver
) and Writable Memory(info->memwritable
), the "Writable Memory" is the memory blocks which are marked as "Private_Dirty" in /proc/PID/smaps file.Other than linux system, could be different way according to gnome-system-monitor code.
You can get private dirty and private clean RSS from /proc/pid/smaps
For a question that mentioned Freebsd, surprised no one wrote this yet :
If you want a linux style /proc/PROCESSID/status output, please do the following :
Atleast in FreeBSD 7.0, the mounting was not done by default ( 7.0 is a much older release,but for something this basic,the answer was hidden in a mailing list!)