For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > January 2005 > a small perl/tk memory tracking module (linux only)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2005-01-28, 3:59 pm


--=-aXZHxMsTt6MkiZ8NSshC
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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

--=-aXZHxMsTt6MkiZ8NSshC
Content-Disposition: attachment; filename=MemoryUse.pm
Content-Type: text/plain; name=MemoryUse.pm; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

########################################
##################################
## MemoryUse Perl Module :
##
## For tracking memory leaks
##
## Author : Christophe Mertz mertz@intuilab.com ( @ 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

--=-aXZHxMsTt6MkiZ8NSshC--

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server. If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to majordomo@lists.stanford.edu

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com