Coder Perfect

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


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).

for processes that you’d like to keep track of 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:

    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/ --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 indicates the amount of physical memory that has been utilized, whereas the VIRT column indicates the amount of virtual memory that has been used, 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.


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

Answered by Sunil Bojanapally

Post is based on