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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-14-2010, 11:01 AM
|
#1
|
Member
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724
Rep: 
|
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.
|
|
|
08-14-2010, 11:18 AM
|
#2
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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!
|
|
|
08-14-2010, 12:07 PM
|
#3
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
08-14-2010, 12:23 PM
|
#4
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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!
|
|
|
08-14-2010, 12:41 PM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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).
|
|
|
08-14-2010, 10:16 PM
|
#6
|
Member
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724
Original Poster
Rep: 
|
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.
|
|
|
08-15-2010, 03:31 AM
|
#7
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
08-15-2010, 03:47 AM
|
#8
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,378
|
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.
|
|
|
08-15-2010, 08:56 AM
|
#9
|
Member
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724
Original Poster
Rep: 
|
Quote:
Originally Posted by syg00
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 02:23 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|