Linux – Which linux command to use to know how much RAM consumed by each process

centoslinux

I have tried top and PS command but i am not able to find the RAM used by each process.

top command says that 240MB RAM used but the Memory shows 0% for all the processes, same with ps. i want to know which process consumes all 240MB RAM.

Is there any command which / script which can sort the running process in oder of increasing RAM usage so that i can see. Also i find it very hard to read bytes and KB. Is there any to way to chnage those units to MB

Please help

Best Answer

ps -eo rss,pid,user,command --sort -size | \
awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | \
egrep -v 0.00

prints:

     6.65 Mb /usr/sbin/mysqld
     0.75 Mb rsyslogd -c4
    38.59 Mb /usr/sbin/apache2 -k start
    37.95 Mb /usr/sbin/apache2 -k start
    34.38 Mb /usr/sbin/apache2 -k start
    33.35 Mb /usr/sbin/apache2 -k start
    31.43 Mb /usr/sbin/apache2 -k start
     2.38 Mb /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock
     0.61 Mb /usr/sbin/nova-agent -q -p /var/run/nova-agent.pid -o /var/log/nova-agent.log -l info /usr/share/nova-agent/nova-agent.py
     3.00 Mb /usr/sbin/apache2 -k start
     1.71 Mb sshd: root@notty
     0.36 Mb sshd: root@pts/0
     1.10 Mb ps -eo rss,pid,user,command --sort -size
     1.40 Mb /usr/lib/openssh/sftp-server
     0.59 Mb /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 103:105
     0.08 Mb /usr/sbin/sshd -D
     0.21 Mb /sbin/init
     1.18 Mb -bash
     0.28 Mb cron
     0.88 Mb qmgr -l -t fifo -u

Keep in mind that free and top will show "cached" memory so you won't be able to tell how much is actually used and free. It's technically free to the system, though it might have to dump the cache to use it. I find free not so useful, and top only slightly more useful (plus top usually consumes any free RAM I have lol).

Related Question