a small perl/tk memory tracking module (linux only)

Christophe Mertz <[email protected]>
Newsgroups gmane.comp.lang.perl.tk
Organization IntuiLab
Message-ID <1106925156.3659.96.camel@datura>
Hello,

Because I have to track some memory leaks, I developed a tiny perl/tk
module to track memory usage (from an external point of view).
It just keep reading the /proc/<pid>/status every half second to get the
data memory and total memory used by the process.

It can be used the following way:

perl -MMemoryUse myscript.pl


I guess this could be useful for others and may be extended as well for
other plateforms (winXP?)

PS: it not yet documented, but you may provide some intersting
feedback...

-- Christophe Mertz
MemoryUse.pm (text/plain, 2 KB)
##########################################################################
##        MemoryUse Perl Module :
##
##        For tracking memory leaks
##
##        Author : Christophe Mertz [email protected] ( @ balma . fr )
##

package Memory;

use vars qw( $VERSION );
($VERSION) = sprintf("%d.%02d", q$Revision: 1.57 $ =~ /(\d+)\.(\d+)/);

use strict;
use vars qw(@ISA);

use Tk;

my $pid = $$;

my $dataLabel;
my $totalLabel;

my $dataSeuil=0;
my $totalSeuil=0;


## creating the window with memory consomption display and a reset button
sub Memory::init {
  my $mw = MainWindow->new();
  my $top = $mw->Frame->pack(-side => 'top');
  my $left = $top->Frame->pack(-side => 'left');
  my $right = $top->Frame->pack(-side => 'left');
  $left->Label(-text => 'Total:')->pack;
  $left->Label(-text => 'Data:')->pack;
  $totalLabel = $right->Label(-text => 'xxMB')->pack;
  $dataLabel = $right->Label(-text => 'xxMB')->pack;

  my $bottom= $top->Frame->pack(-side => 'bottom');
  $bottom->Button(-text => 'reset', -command => \&seuilReset )->pack;

  &displayMemoryUsage;
  $mw->repeat(1000,\&displayMemoryUsage);
}


sub seuilReset {
  ($totalSeuil,$dataSeuil) = &getMemoryUsage;
  $dataLabel->configure(-text =>  "0 KB");
  $totalLabel->configure(-text => "0 KB");
}


sub displayMemoryUsage {
  my ($total,$data) = &getMemoryUsage;
  $dataLabel->configure(-text => ($data-$dataSeuil) . " KB");
  $totalLabel->configure(-text => ($total-$totalSeuil) . " KB");
}

sub getMemoryUsage {
  open (PROC, "/proc/$pid/status");
  my ($totalMemory,$dataMemory);
  while (<PROC>) {
    if (/^VmSize:\s+(\d+)/) {
      $totalMemory = $1;
    }
    elsif (/^VmData:\s+(\d+)/) {
      $dataMemory = $1;
      last;
    }
  }
  close PROC;
  return ($totalMemory,$dataMemory);
}

Memory::init;

1;

__END__

    
=head1 NAME

Memory - blabla ... a perl module for tracking memory consomption 


=head1 SYNOPSIS

 perl -MMemoryUse myscript.pl

=head1 DESCRIPTION

to be done

=head1 AUTHOR

Christophe Mertz <mertz at intuilab dot com>

=head1 HISTORY

December 2004 : creation
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.