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?
You can check memory of a process or a set of processes in human readable format (in KB or kilobytes) with pmap command. All you need is the PID of the processes you want to check memory usage of. As you can see, the total memory used by the process 917 is 516104 KB or kilobytes.
The Linux 'top' command is the best and widely used command that everyone uses to monitor Linux system performance. It displays a real-time view of the system processes running on the interactive interface. You should run the top command in batch mode to find out top memory consuming processes in Linux.
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 = 3MiBOther 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With