If I run top -c
or htop
I get processes with their memory consumption, but it is not what I want to see.
Is there a command line that allows to see the RAM consumption of a given application ?
For example, I want to see the RAM consumption of Apache web server (not by checking all the processes it runs, instead)
-
1Look at this https://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process – TuKsn May 20 '14 at 17:07
-
Thanks. The best i can find in it is a memory consumption by process, which thing top -c already gives me – May 20 '14 at 17:12
2 Answers
There's a very good detailed explanation here: https://blogs.kde.org/2005/09/15/measuring-memory-usage
But essentially: You have to really dig in and understand how the application is set up.
So for example, looking at mysql:
PID PPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6004 16116 composit 20 0 37900 27m 2908 S 0 0.2 0:40.33 mysqld
16115 16085 composit 20 0 37900 27m 2908 S 0 0.2 0:00.37 mysqld
16116 16115 composit 20 0 37900 27m 2908 S 0 0.2 2:07.34 mysqld
16117 16116 composit 20 0 37900 27m 2908 S 0 0.2 0:00.00 mysqld
16118 16116 composit 20 0 37900 27m 2908 S 0 0.2 3:19.79 mysqld
16119 16116 composit 20 0 37900 27m 2908 S 0 0.2 0:00.01 mysqld
16120 16116 composit 20 0 37900 27m 2908 S 0 0.2 5:31.09 mysqld
16121 16116 composit 20 0 37900 27m 2908 S 0 0.2 14:19.53 mysqld
16122 16116 composit 20 0 37900 27m 2908 S 0 0.2 36:13.67 mysqld
16123 16116 composit 20 0 37900 27m 2908 S 0 0.2 30:30.64 mysqld
16124 16116 composit 20 0 37900 27m 2908 S 0 0.2 0:00.15 mysqld
16493 16116 composit 20 0 37900 27m 2908 S 0 0.2 0:00.00 mysqld
The total memory used is about 25 MB (Take the 27 MB RES and subtract the shared (SHR))
I validated this by checking the total memory usage (free -m, +/ buffers/cache) before and after issuing a "killall mysqld". After killing all mysqld processes, the memory usage dropped by 25 MB according to "free -m".
If you see that each process has identical VIRT, RES, and SHR columns, they are likely just threads of the same process. (Older Linux libraries handled threading by spawning multiple real processes that essentially occupied the same memory)
If they are different, you might be able to estimate it by doing a SUM of (RES - SHR). But that only works if the processes are in fact separate processes and not just threads of the same process. Looking at the PPID (Parent Process ID) also helps. If they all have the same parent, they are probably just threads (Though not necessarily).
Unfortunately there's no real good easy way to answer this in Linux. The only easy way is to check "free" immediately before terminating the process and check it again immediately after. look at the "-/+ buffers/cache:" line and see how much memory usage decreased and that will tell you how much it was using.

- 96
With a bit of help from man ps
and this great answer...
ps -up $(pidof PROCESS_NAME)
For example:
[wilf@comp ~]$ ps -up $(pidof firefox)
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
wilf 1619 8.9 5.6 1464216 342396 ? Sl 17:56 1:36 /usr/lib64/firefox/firefox
[wilf@comp ~]$
Other ways:
cat /proc/$(pidof firefox)/status | grep VmSize
This can be run without -x
and tail -1
:
pmap -x $(pidof firefox) | tail -1
only shows percentage:
top -p $(pidof firefox)
Also, whilst using top
you can press i to ignore idle/zombie processes to make it easier to read:
-
but an application launches several processes, how can I know which one is the main ? – May 20 '14 at 17:20
-
guess? i think
pmap
shows lots of stuff about resources the PID is using. The information is provided based on the name of the process and the PID found from it usingpidof
, so the main process shown if whatever name you provide (i think, don't know whether it refers back to parent processes) – Wilf May 20 '14 at 17:29 -
2This separates output by process. You won't get the total amount of memory used by all the Apache processes this way. You need to sum up, but counting shared regions only once (this is the difficult bit). – Gilles 'SO- stop being evil' May 20 '14 at 18:12