This is a module to use in X, to monitor a script's memory usage. It opens a little window in the lower right corner, and displays the calling script's pid and memory usage. This is a Gtk2 version, which is similar to my Tk version at linux memory leak monitor. An intersting difference between Tk and Gtk2 can be seen. Tk allows you to fork and open a new window. Gtk2 requires that you fork-&-exec in order that the X socket connections of the windows don't interfere with each other and crash. (credits to muppet for pointing that out to me). If you use the asmutils version of cat, it is only 147 bytes.

You can click the GMeM window to kill it.

UPDATE May-8-2005 With another thanks to muppet, for pointing it out, I changed "use Gtk2 -init" to "eval use Gtk2 -init". This prevents the calling program from unnecesarily loading Gtk2 modules, if it is not Gtk2 based ( i.e. a commandline program or a Tk app).

package GMeM; use warnings; use strict; #save this file as GMeM.pm and place in your PERL5LIB my $pid =$$; #gets the caller script's pid and #passes it thru the fork-&-exec below #only gets created after fork and exec to prevent #module's X socket connection from interfering with #calling program's X socket connection if ((defined $ARGV[0]) and ($ARGV[0] eq 'GMeM')) { #use Gtk2 '-init'; #BAD unnecessarily loads modules eval "use Gtk2 -init;"; $pid = $ARGV[1]; my $blueh = Gtk2::Gdk::Color->new (0,0xFFFF,0xFFFF); my $bluel = Gtk2::Gdk::Color->new (0,0xCCCC,0xFFFF); Gtk2::Rc->parse_string(<<___EOS); style "normal" { font_name ="Sans Bold 12" } widget "*" style "normal" ___EOS my $win = Gtk2::Window->new('popup'); $win->signal_connect( 'destroy' => \&delete_event ); my ($xscr, $yscr) = (Gtk2::Gdk->screen_width, Gtk2::Gdk->screen_he +ight); $win->move($xscr - 180, $yscr - 20); my $but = Gtk2::Button->new("PID: $pid-> " ); $but->modify_bg ('normal', $blueh); $but->modify_bg ('prelight', $bluel); $but->signal_connect( 'clicked' => \&delete_event ); $win->add($but); $0 = "GMeM $pid"; $win->show_all(); &refresh($pid); my $repeater = Glib::Timeout->add(1000, sub{ refresh($pid) ; 1;} ) +; Gtk2->main; ##################################### sub delete_event { Gtk2->main_quit; return 0; } sub refresh { my $pid = shift; #asmutils version of cat #my @size = split "\n", `/home/zentara/perl5lib/cat /proc/$pid +/status`; my @size = split "\n", `cat /proc/$pid/status`; (my $vmsize) = grep { /VmSize/ } @size; my (undef, $size) = split ' ', $vmsize; $but->set_label("PID: $pid -> $size"); if ($size eq '') { exit } } #################################### } elsif (fork() == 0) { exec "$^X -MGMeM -e1 GMeM $pid"; } # thanks to muppet for this cleverness 1;