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 thoughts on “A clever memory script in perl

  1. Mike McC.

    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.

    Reply
  2. Gabe

    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?

    Reply
  3. Jose Catre-Vandis

    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]

    Reply
  4. Gabe

    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)

    Reply

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