How much memory does a process use on Linux?
Sometimes the easy questions are the hardest to answer.
Memory can mean RSS (Resident Set Size) which is the memory of a process held in RAM (so not swapped out). That does include shared memory allocations. So if you add two RSS numbers, you're probably wrong already. Still this is usually the number we look for in most practical investigations.
Then there is VSZ (Virtual Set siZe) also called SIZE. The VSZ includes code, data and stack segments a process has allocated. And again that will count some shared address space. So usually bash will have a VSZ that's lower than its RSS.
man ps
will also tell you:
The SIZE and RSS fields don't count some parts of a process including the page tables, kernel stack, struct
thread_info, and struct task_struct. This is usually at least 20 KiB of memory that is always resident.
In most (if not all) practical scenarios that difference won't matter. If it were, you'd be using valgrind
to look into the memory usage of your application in minute detail. Wouldn't you?
If you want to have an as-detailed-as-possible look into the memory allocations of a process pmap <pid>
will give you the information.
The summary at the end is a gross over-estimation of the total memory a process has allocated as it counts all mapped memory (and may still be wrong due to de-duplication and other factors). But that number may well serve as an upper bound if you need something like that.
For running processes
ps -eo 'pid user rss:8 size:8 cmd' --sort 'rss'
will give you a nice sorted list of processes and their RSS and VSZ (SIZE) in kiB (old school kB...).
For short running commands GNU time
(not the bash build-in time
command, apt install time
on Debian-based systems) has a nice capability that's not widely known yet:
/usr/bin/time -f "RSS: %MkiB" <command>
will tell you the maximum RSS size the <command>
has had during its lifetime. That's better than top
or watch ps
and trying to spot the process.
Comments
Display comments as Linear | Threaded
Kristen on :
Thanks for the tips!
Altbalaji on :
I’ve recently started a website, the information you provide on this web site has helped me tremendously. Thanks for all of your time & work.
Beverly Kadow on :
Saved as a favorite! I enjoy your site! Thank you very much!