LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2010, 11:01 AM   #1
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
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.
 
Old 08-14-2010, 11:18 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.

Last edited by GrapefruiTgirl; 08-14-2010 at 11:28 AM. Reason: changed print to printf - oops!
 
Old 08-14-2010, 12:07 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
1 members found this post helpful.
Old 08-14-2010, 12:23 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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!
 
Old 08-14-2010, 12:41 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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).
 
Old 08-14-2010, 10:16 PM   #6
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Original Poster
Rep: Reputation: 101Reputation: 101
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

Last edited by quanta; 08-19-2010 at 11:09 PM.
 
Old 08-15-2010, 03:31 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 08-15-2010, 03:47 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 08-15-2010, 08:56 AM   #9
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Original Poster
Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by syg00 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
top command output in "human readable" custangro Linux - General 7 12-01-2017 11:53 AM
Human readable IP adresses from netfilter dividedby0 Linux - Networking 3 03-22-2009 12:10 PM
Java Epoch time to Human Readable (using Date) true_atlantis Programming 1 05-09-2008 05:13 PM
top don't show me all my ram Polyvore Linux - General 2 04-29-2004 02:00 AM
Why does top show RAM almost all used up Apollo77 Linux - General 10 12-30-2003 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration