LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to show the top processes eating RAM in human readable? (https://www.linuxquestions.org/questions/programming-9/how-to-show-the-top-processes-eating-ram-in-human-readable-826216/)

quanta 08-14-2010 11:01 AM

How to show the top processes eating RAM in human readable?
 
Hi folks,

Sometimes, I want to find out which process is eating RAM, I use this command:
Code:

ps -eo size,pid,user,command | sort -k1 -rn | head -10
but it displays with no human readable:
Code:

283364 4644 quanta  /usr/lib/mozilla-firefox/firefox --sm-config-prefix /firefox-C3JYUC/ --sm-client-id 1014cd7d2d4000128169799000000044950019 --screen 0
230372 3635 mysql    /usr/sbin/mysqld --defaults-file=/etc/mysql/my.cnf --basedir=/usr --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock
78836  4618 quanta  /usr/bin/knotify4
41816  4142 104      /usr/bin/memcached -d -p 11211 -U 11211 -m 64 -c 1024 -u memcached -P /var/run/memcached/memcached-11211.pid
36296  4345 root    /usr/bin/X -br -novtswitch -quiet -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-30y3We
34464  3441 root    /usr/sbin/rsyslogd -c3 -i /var/run/rsyslogd.pid -f /etc/rsyslog.conf
25860  5267 root    sort -k1 -rn
20780  4619 quanta  /usr/bin/plasma-desktop
13900  3910 root    /usr/sbin/console-kit-daemon
12732  2817 root    /usr/bin/python2.6 -O /usr/share/wicd/daemon/wicd-daemon.py

Can any one give me some suggestions to use 'awk' to convert 'size' column to human readable?

I also read this thread but I get stuck in printing from $4 to NR.

GrapefruiTgirl 08-14-2010 11:18 AM

Here's a start:

Not sure if you wish to use awk to print records from 4 to NR, or fields from 4 to NF, so here's a sample of each:
Code:

# records:
root@reactor: echo "record1
record2
record3
record4
record5
record6
record7" | awk  '{ if (NR>=4) print $0}'

record4
record5
record6
record7
root@reactor:

# fields:
root@reactor: echo "a b c d e f g" | awk -F " " '{ for (x=4;x<=NF;x++) {printf $x" "}; print "\n"}'
d e f g

root@reactor:

I'll leave the conversion to 'human readable' to someone else. ;)

druuna 08-14-2010 12:07 PM

Hi,

Adding human readability (I hope this is what you are after), building on GrapefruiTgirl's code:

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

Sample output (cut down in size):
Code:

    0.644531 Mb bash
    0.644531 Mb bash
    0.812500 Mb /usr/sbin/ntpd
    0.906250 Mb /usr/bin/xterm -geometry 230x90+0+0
    1.117188 Mb sendmail: accepting connections
    1.148438 Mb /usr/bin/xterm -geometry 146x90+0+0 -title -= System I =-
    1.214844 Mb xfce-mcs-manager
    1.386719 Mb /usr/bin/xterm -geometry 80x51-3+0
    1.519531 Mb xfdesktop
    2.554688 Mb xfwm4 --daemon
    4.492188 Mb xfce4-panel
    49.632812 Mb /usr/local/lib/opera//operapluginwrapper 29 32 /usr/local/lib/opera/plugins/libflashplayer.so
  152.589844 Mb /usr/local/lib/opera/opera -geometry 1420x1182+0+0 -nomail -nolirc
  462.632812 Mb /usr/bin/pan

Hope this helps.

GrapefruiTgirl 08-14-2010 12:23 PM

Hey druuna, that's nice :) I think I'll keep that code close at hand in case I need it sometime.

FWIW I think I'd have sorted it the other way, from biggest consumer to smallest, but OP can decide on that for him/herself.

Cheers!

druuna 08-14-2010 12:41 PM

Hi,

Thanks :)

BTW: I was cautious with the %13.6f part. This leaves room for 6 decimals before the point.

Might be too much, 12.6 is probably better, 11.6 might be pushing it a bit (although: I don't know of any processes that take more then 9999 Mb....). Increased precision is also possible: Increase both parts with 1 (11.6 -> 12.7 -> 13.8).

quanta 08-14-2010 10:16 PM

Based on all of your code, I have a better script for myself:
Code:

ps -eo size,pid,user,command | sort -rn | head -10 | awk '{
> hr[1024**2]="GB"; hr[1024]="MB";
> for (x=1024**3; x>=1024; x/=1024) {
> if ($1>=x) { printf ("%-6.2f %s ", $1/x, hr[x]); break }
> } } { printf ("%-6s %-10s ", $2, $3) }
> { for ( x=4 ; x<=NF ; x++ ) { printf ("%s ",$x) } print ("\n") }
> '

and result:
Code:

267.90 MB 4611  quanta    /usr/lib/mozilla-firefox/firefox --sm-config-prefix /firefox-TFaXPS/ --sm-client-id 1014cd7d2d4000128211797800000044960013 --screen 0

224.97 MB 3615  mysql      /usr/sbin/mysqld --defaults-file=/etc/mysql/my.cnf --basedir=/usr --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock

76.38  MB 4570  quanta    /usr/bin/knotify4

47.87  MB 6848  quanta    /usr/bin/pidgin

40.84  MB 4120  104        /usr/bin/memcached -d -p 11211 -U 11211 -m 64 -c 1024 -u memcached -P /var/run/memcached/memcached-11211.pid

38.92  MB 4314  root      /usr/bin/X -br -novtswitch -quiet -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-ghYTqh

33.66  MB 3421  root      /usr/sbin/rsyslogd -c3 -i /var/run/rsyslogd.pid -f /etc/rsyslog.conf

25.25  MB 9832  quanta    sort -rn

22.74  MB 4588  quanta    /usr/bin/plasma-desktop

13.57  MB 3888  root      /usr/sbin/console-kit-daemon

I used 'sort' and 'head' before processing and I think you should use 'MB' (Megabyte) instead of 'Mb' (Megabit).

Thanks all.

Edited: because of the memory size displays in kilobytes, I edited my above code to hr[1024**2]="GB"; hr[1024]="MB";, it also match with result from free -m:
Code:

            total      used      free    shared    buffers    cached
Mem:          2014        768      1246          0        29        401
-/+ buffers/cache:        337      1677
Swap:        1023          0      1023


druuna 08-15-2010 03:31 AM

Hi,

Nice to see that our hints helped you create your own solution :)

Two things I would like to mention:

- If it bothers you that there are blank lines between each entry (might be on purpose, don't know) then change the last print statement print ("\n") into print " "

- If a one-liner reaches this size/length it is in general more clear if you make a small script (other up-side: you can re-use it).

BTW: You're welcome.

syg00 08-15-2010 03:47 AM

I'll just add that ps will allow you to sort (even reverse) on any column - not to mention --no-header.

Personally I don't have a need for (so called) human readable, so no need for external calls.

quanta 08-15-2010 08:56 AM

Quote:

Originally Posted by syg00 (Post 4066727)
I'll just add that ps will allow you to sort (even reverse) on any column - not to mention --no-header.

Thank you.

Search through the manpage of ps, I find out the --sort [-]key option for sorting in descending order:
Code:

$ ps -eo size,pid,user,command --sort -size | head -10


All times are GMT -5. The time now is 03:01 AM.