Say I've got a process using 200MB of memory, and it fork()s:
python -c "import os; data='x'*200000000; os.fork(); raw_input()"
Programs like 'top' will show each process using 200MB, with very little SHRd memory, so it appears as if the processes are using 400MB in total. However, because fork() implements copy-on-write (COW) for the process's memory pages, the reality is that the processes are only using 200MB in total.
Why doesn't top show what fraction of the memory is COW? Is there a way to make it do so? Or is there another command I can use instead?
Note: 'top' on OSX seems to have an RSHRD column that does what I'd expect. My question is for Linux.
Best Answer
You can get that sort of information from the
/proc/<pid>/smaps
file for each process in the form of thePss
entry (short for "Proportional share size").In the above example with 200MB "shared" between two processes, each process would show 100MB in the PSS entry for that mapping, i.e. the memory is distributed evenly between the processes that share it (until it gets copied and unshared by either process).
Here's an extract from running something like what you posted:
(There's a lot of stuff in those files, including mappings for shared libraries that are potentially shared between many processes, and thus each process gets only a small part accounted in its PSS entry for those.)
Here's a nice article about this: ELC: How much memory are applications really using?
I don't know of a common top-like tool to display this information, and I don't think
ps
has options to show that either unfortunately. The article points to a repository with python scripts calledpagemap
by Matt Mackall that you could use or adapt though.Shameless plug: you'll find a few posts on Unix & Linux about PSS and the
smaps
file if you're interested in that.