A clever memory script in perl

About a year and a half ago, I mentioned a quick-draw python script that shows the amount of memory allocated to each running program, and shows you the results in a cute little table.

Well, maybe not cute. :roll:

Gabe sent an e-mail the other day with simpler, quicker perl script that does something similar, but relies only on top and some shrewd sed-like action to whittle the results down.

And those results look something like …

Mem% Cmd [num instances]

9.5 bash [5]
8.7 elinks [2]
8.3 alpine [1]
6.5 centerim [1]
4.9 ssh [2]
3.6 mc [1]
3.6 vim [1]
3.4 wyrd [1]
2.8 screen [2]
2.4 udevd [3]

With allowances given for WordPress.com’s mangling of code boxes. :evil: :roll:

Not exactly a mimic of the other script, but not intended to be. This shows memory percentage, but could probably be adjusted to show any of the information top allows. Rather clever, actually. :)

Gabe has given his permission to cut-and-paste this one, so here are the important bits:

#!/usr/bin/perl

# topsum v0.1 - sum multiple instances from top to get cumulative memory usage

@topsum = `top -n 1 -b`;

foreach $line (@topsum) {
    if ($inlist == 1) {
	@curline = split(/\s+/,$line);
	$cumulativemem{$curline[-1]}[0] += $curline[-3];
	++$cumulativemem{$curline[-1]}[1];
    }
    $inlist = 1 if ($line =~ /^\s+PID/);
}

print "\n\tMem%\tCmd [num instances]\n\n";
foreach (sort {$cumulativemem{$b}[0] <=> $cumulativemem{$a}[0]} keys %cumulativemem) {
    print "\t$cumulativemem{$_}[0]\t$_ \[$cumulativemem{$_}[1]\]\n" if ($cumulativemem{$_} > 0);
    ++$dummy;
    if ($dummy == 10) {print "\n"; last}
}

The nicest part about this script? You don’t need root permission to use it. A small bonus, from my perspective. :)

About these ads

4 Responses to “A clever memory script in perl”


  1. 1 Mike McC. 2011/01/04 at 6:00 PM

    Wow… some really interesting (disgusting) results from my Ubuntu machine. nautilus taking nearly half a gig isn’t too shocking, but my fun little geyes_applet2 is sitting on 10M? yikes.

  2. 2 Gabe 2011/01/05 at 4:39 AM

    For some reason this script doesn’t work with watch. Can anyone figure out why? It runs, but the values are all wrong. I think it might have to do with ‘watch’ passing its arguments to sh, but if I start an sh session and run ‘topsum’ it works fine. Any ideas?

  3. 3 Jose Catre-Vandis 2011/01/05 at 5:11 AM

    Here’s mine:

    Mem% Cmd [num instances]

    6.2 firefox-bin [1]
    2.5 skype.real [1]
    1.5 Xorg [1]
    1.4 blueman-applet [1]
    1.3 ubuntuone-syncd [1]
    1.3 apache2 [6]
    1.2 Thunar [1]
    1.2 ubuntu-sso-logi [1]
    1.1 plugin-containe [1]
    1 applet.py [1]

  4. 4 Gabe 2011/01/05 at 5:20 AM

    FWIW, here’s mine:

    Mem% Cmd [num instances]

    13.5 apache2 [6]
    3.9 mysqld [1]
    2.7 NetworkManager [1]
    1.5 postgres [5]
    1 evolution-data- [1]
    1 named [1]
    0.7 winbindd [4]
    0.7 smbd [2]
    0.6 getty [6]
    0.6 sshd [3]

    (768MB total RAM, Debian Lenny, serving WordPress)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Welcome!



Visit the Wiki!

Some recent desktops


May 6, 2011
Musca 0.9.24 on Crux Linux
150Mhz Pentium 96Mb 8Gb CF
 


May 14, 2011
IceWM 1.2.37 and Arch Linux
L2300 core duo 3Gb 320Gb

Some recent games


Apr. 21, 2011
Oolite on Xubuntu 11.04
L2300 core duo 3Gb 320Gb

Enter your email address to subscribe to this blog and receive notifications of new posts.

Join 405 other followers

License

This work is licensed under the GNU Free Documentation License. Please see the About page for details.

Blog Stats

  • 3,958,532 hits

Archives


Follow

Get every new post delivered to your Inbox.

Join 405 other followers

%d bloggers like this: