Coder Perfect

How can I tell how much memory a process is using in Linux?

Problem

In my LAMP application, I believe I have a memory leak (memory gets used up, swap starts getting used, etc.). It might help me fix my problem if I could see how much RAM the various processes are utilizing. Is there a method for me to look at this data in *nix?

Asked by StackOverflowNewbie

Solution #1

It’s more difficult than it appears to be to get the proper memory utilization. The best method I could come up with is:

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

Where “PROCESS” is the name of the process you’d like to examine, and “TYPE” is one of the following:

Size (i.e. virtual size, which is almost useless) and Referenced are two other permissible variables (the amount of memory currently marked as referenced or accessed).

To keep an eye on those numbers for processes you want to monitor, you can use watch or some other bash-script-fu.

http://www.kernel.org/doc/Documentation/filesystems/proc.txt for further information about smaps.

Answered by Giuseppe Cardone

Solution #2

I’m not sure why the answer appears to be so complicated… This appears to be a fairly simple task 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

Answered by Greg Slepak

Solution #3

To find the application’s process id, use ps, then 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.

Using the search term “man top,” you can learn more.

Answered by Zaz

Solution #4

To begin, obtain the pid:

ps ax | grep [process name]

And then:

top -p PID

You can keep an eye on multiple processes at the same time:

top -p PID1 -p PID2 

Answered by Peter

Solution #5

To report memory use, use the pmap command.

Synopsis:

pmap [ -x | -d ] [ -q ] pids... 

Answered by Sunil Bojanapally

Post is based on https://stackoverflow.com/questions/3853655/in-linux-how-to-tell-how-much-memory-processes-are-using