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
More elegant approach:
echo "Memory usage for PID <>:"; for mem in {Private,Rss,Shared,Swap,Pss};do grep $mem /proc/<pid>/smaps | awk -v mem_type="$mem" '{i=i+$2} END {print mem_type,"memory usage:"i}' ;done
Use top
or htop
and pay attention to the "RES" (resident memory size) column.
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
Why all these complicated answers with various shell scripts? Use htop, it automatically changes the sizes and you can select which info you want shown and it works in the terminal, so it does not require a desktop. Example: htop -d8
Thanks. I used this to create this simple bash script that can be used to watch a process and its memory usage:
$ watch watchmypid.sh
#!/bin/bash
#
PROCESSNAME=changethistoyourprocessname
MYPID=`pidof $PROCESSNAME`
echo "=======";
echo PID:$MYPID
echo "--------"
Rss=`echo 0 $(cat /proc/$MYPID/smaps | grep Rss | awk '{print $2}' | sed 's#^#+#') | bc;`
Shared=`echo 0 $(cat /proc/$MYPID/smaps | grep Shared | awk '{print $2}' | sed 's#^#+#') | bc;`
Private=`echo 0 $(cat /proc/$MYPID/smaps | grep Private | awk '{print $2}' | sed 's#^#+#') | bc;`
Swap=`echo 0 $(cat /proc/$MYPID/smaps | grep Swap | awk '{print $2}' | sed 's#^#+#') | bc;`
Pss=`echo 0 $(cat /proc/$MYPID/smaps | grep Pss | awk '{print $2}' | sed 's#^#+#') | bc;`
Mem=`echo "$Rss + $Shared + $Private + $Swap + $Pss"|bc -l`
echo "Rss " $Rss
echo "Shared " $Shared
echo "Private " $Private
echo "Swap " $Swap
echo "Pss " $Pss
echo "=================";
echo "Mem " $Mem
echo "=================";
Use
Example:
ps-of()
{
ps u `pidof "$@"`
}
$ ps-of firefox
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
const 18464 5.9 9.4 1190224 372496 ? Sl 11:28 0:33 /usr/lib/firefox/firefox
$ alias ps-mem="ps xu --sort %mem | sed -e :a -e '1p;\$q;N;6,\$D;ba'"
$ ps-mem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
const 3656 0.0 0.4 565728 18648 ? Sl Nov21 0:56 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon
const 11361 0.3 0.5 1054156 20372 ? Sl Nov25 43:50 /usr/bin/python /usr/bin/ubuntuone-control-panel-qt
const 3402 0.0 0.5 1415848 23328 ? Sl Nov21 1:16 nautilus -n
const 3577 2.3 2.0 1534020 79844 ? Sl Nov21 410:02 konsole
const 18464 6.6 12.7 1317832 501580 ? Sl 11:28 1:34 /usr/lib/firefox/firefox
$ ps h -o pmem -C firefox
12.7