In Linux, how to tell how much memory processes are using?

131,553

Solution 1

Getting right memory usage is trickier than one may think. The best way I could find is:

echo 0 $(awk '/TYPE/ {print "+", $2}' /proc/`pidof PROCESS`/smaps) | bc

Where "PROCESS" is the name of the process you want to inspect and "TYPE" is one of:

  • Rss: resident memory usage, all memory the process uses, including all memory this process shares with other processes. It does not include swap;
  • Shared: memory that this process shares with other processes;
  • Private: private memory used by this process, you can look for memory leaks here;
  • Swap: swap memory used by the process;
  • Pss: Proportional Set Size, a good overall memory indicator. It is the Rss adjusted for sharing: if a process has 1MiB private and 20MiB shared between other 10 processes, Pss is 1 + 20/10 = 3MiB

Other valid values are Size (i.e. virtual size, which is almost meaningless) and Referenced (the amount of memory currently marked as referenced or accessed).

You can use watch or some other bash-script-fu to keep an eye on those values for processes that you want to monitor.

For more informations about smaps: http://www.kernel.org/doc/Documentation/filesystems/proc.txt.

Solution 2

I don't know why the answer seem so complicated... It seems pretty simple to do this with ps:

mem()
{                                                                                                      
    ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $@ | awk '{printf $1/1024 "MB"; $1=""; print }'
}

Example usage:

$ mem mysql
0.511719MB 781 root /bin/sh /usr/bin/mysqld_safe
0.511719MB 1124 root logger -t mysqld -p daemon.error
2.53516MB 1123 mysql /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306

Solution 3

Use ps to find the process id for the application, then use top -p1010 (substitute 1010 for the real process id). The RES column is the used physical memory and the VIRT column is the used virtual memory - including libraries and swapped memory.

More info can be found using "man top"

Solution 4

First get the pid:

ps ax | grep [process name]

And then:

top -p PID

You can watch various processes in the same time:

top -p PID1 -p PID2 

Solution 5

You can use pmap to report memory usage.

Synopsis:

pmap [ -x | -d ] [ -q ] pids... 
Share:
131,553

Related videos on Youtube

StackOverflowNewbie
Author by

StackOverflowNewbie

Updated on July 05, 2022

Comments

  • StackOverflowNewbie
    StackOverflowNewbie over 1 year

    I think I may have a memory leak in my LAMP application (memory gets used up, swap starts getting used, etc.). If I could see how much memory the various processes are using, it might help me resolve my problem. Is there a way for me to see this information in *nix?

    • PatS
      PatS almost 2 years
      Can you update the question to indicate the OS that you were using at the time? The selected answer doesn't seem to work 11 years later. Ah, nevermind.
  • StackOverflowNewbie
    StackOverflowNewbie over 13 years
    by the time I can execute top -pXXXX, the process is already done. So, I get nothing. Suggestions?
  • StackOverflowNewbie
    StackOverflowNewbie over 13 years
    I see the RES, but I don't think I need that. My used Mem and used Swap keeps going up. I need to know what's making those go up. Ideas?
  • Gunther Piez
    Gunther Piez over 13 years
    Resident memory is the memory used by your processes. If none of the processes seems to be using much memory in spite of your total memory usage increasing, the memory could only be used by the kernel. Try sorting after the RES column. Another point maybe too high swappiness when you have heavy disk IO.
  • Gunther Piez
    Gunther Piez over 13 years
    Regarding "VIRT": For almost all practical purposes, the size of the virtual image tells you nothing - almost every linux system is configured to allow overcomitting of memory and a lot of apps actually do heavy overcommit.
  • Matt Phillips
    Matt Phillips almost 11 years
    This is terrific, however it looks like it returns memory in KB (for Rss and Private anyway). Do you know how to get memory in bytes?
  • Timothée Groleau
    Timothée Groleau almost 11 years
    likely because the script computes the data with 5 passes through of the smaps file. It should be reasonably easy to have awk do parsing and computation in one pass.
  • Trevor Boyd Smith
    Trevor Boyd Smith over 10 years
    Here's a one liner that allows you to specify the name of the process (assumes there is only one process that matches the name): top -p`ps -ef | grep -i $NAME_OF_PROCESS | grep -v grep | gawk '{print $2}'`
  • zwol
    zwol over 10 years
    Ages later and probably not relevant anymore, but: actual memory allocation is always a multiple of the physical page size, which on modern systems is always a small multiple of 1024 bytes. So just multiply the size in KB by 1024 for bytes; there is no rounding error. (The kernel has mostly not caught the iB disease: unless there is clear evidence to the contrary, assume K = 1024 not 1000.)
  • ylluminate
    ylluminate almost 10 years
    What would be the "total" memory consumed for a scenario such as this: gist.github.com/9bbd0ce953143b67c038 ?
  • Bryan
    Bryan about 9 years
    You can do the cat+grep+awk+sed with just awk: echo 0 $(sudo awk '/TYPE/ {print "+", $2}' /proc/PID/smaps) | bc
  • m3nda
    m3nda almost 9 years
    @TimothéeGroleau Agree with awk to performance, anyway the script looks cool and someone can learn a bit from it. Maybe Paul Rubenstein wanna update their script :D. Thanks.
  • AAAfarmclub
    AAAfarmclub over 8 years
    $ top -p $(pgrep <your process name> | xargs | tr ' ' ',')
  • Calmarius
    Calmarius over 8 years
    It does not work when multiple instances of the same program (eg. chrome) are running.
  • UtahJarhead
    UtahJarhead about 8 years
    If you substitute: `pidof PROCESS` with the actual PID, it will review a single process's RAM usage.
  • UtahJarhead
    UtahJarhead about 8 years
    Most people using the command line are interested in figuring it out programmatically. In some cases, they simply want to learn how to figure it out using the kernel instead of a prebuild process.
  • TiCPU
    TiCPU about 8 years
    Nice one, here is an example usage: pmap $(pgrep -f -u username /usr/bin/gnome-shell) | sed -n -e 's/ total \+//p' | numfmt --from=iec 1724678144
  • Anup Ash
    Anup Ash over 7 years
    An enhancement top ps -ef | grep -i $NAME_OF_PROCESS | grep -v grep | gawk '{print "-p "$2 }'
  • A. Binzxxxxxx
    A. Binzxxxxxx over 6 years
    You do not need to sum it up in most cases its summed up for you /proc/[pid]/status
  • watzon
    watzon over 6 years
    Awesome use of a function. Added this one to my .zshrc
  • James Lai
    James Lai over 6 years
    Why not do it all in awk instead of passing to bc? awk 'BEGIN { used=0 }; /TYPE/ { used += $2 } END { print used }' /proc/PID/smaps will give you the size in KB.
  • AlexT
    AlexT about 6 years
    Handy function. It's worth noting that the rss column used for the calculation (Resident Set Size) includes memory from shared libraries, so will throw the numbers off. In my case, the processes were using more memory than the system had available.
  • THESorcerer
    THESorcerer about 6 years
    this is the answer, dunno why the other was marked as correct, all I get from it is the result "0", this one show exactly what I need ... Thank YOU
  • voxobscuro
    voxobscuro over 5 years
    The macos /usr/bin/time is not capable of this level of analysis, but homebrew does provide the correct utility through the gnu-time package. It installs a utility called gtime which does what you talk about.
  • oml
    oml almost 4 years
    htop -t shows processes in tree view, so you can see the RES memory in tree view.
  • LnxSlck
    LnxSlck about 3 years
    This is the right answer, works flawlessly on Linux
  • Bryan
    Bryan almost 3 years
    For MacOS, I had to use the following (removing the args:100 and --sort %mem options): # utiltiy function to get memory usage mem() { ps -ef -o rss,pid,user | grep -v grep | grep -i "$@" | awk '{printf $1/1024 "MB"; $1=""; print }' }
  • PatS
    PatS almost 2 years
    If this doesn't work for you, try the answer the defines the mem shell function. That is the answer that worked for me. See stackoverflow.com/a/20277787/3281336
  • PatS
    PatS almost 2 years
    I modified it to include **virtual memory size in KiB. Also format numbers in fixed width so results are easier to compare. I also truncate the line length to 110 chars but you can pass a 2nd argument to make line longer (shows more of arguments). The solution is: mem() { maxLineLen=${2:-110} ps -eo rss,vsz,pid,euser,args --sort %mem | grep -v grep | grep -i $1 | cut -c -$maxLineLen | awk '{printf("%9.2fMB %9.2fMB", $1/1024, $2/1024); $1=""; $2=""; print }' }