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;

In reply to GMeM -- a Gtk2 memory monitor utility by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.