Ubuntu Linux: Process swap memory and memory usage
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
by free
. There 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 thefree
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 thefree
number to be larger than thetop
RSS sum.
I think you're better off trusting the output of "free" as far as your total memory usage goes, and trusting "ps" for a general idea of how much memory a single process is using.
Just because the sum of "ps" RSS values doesn't equal "free" doesn't stop you from sorting your processes by RSS and evaluating the biggest ones for killing.
That being said, if all your effort is only in service of making sure the machine can hibernate, creating more swap (in the form of a file on disk, if necessary) is probably an easier path to take.
I finally got the answer to my question. There's a program called smem (on Ubuntu/Debian apt install smem
) that lets you list swap and used memory separately.
A few different ways of listing swap can be found here: https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/.