Programming:Perl:Memory::Stats Get RSS memory reporting on multiple platforms

From Knowledge base
Jump to: navigation, search

It is often nice to be able to dump the current memory usage of a part of your process.

I am working on Mac OSX, and I am frustrating when a Perl module only works on Linux because it read a file in /proc.

Thanks to IRC users, I have discovered Proc::ProcessTable, and then I have decided to create a module that use it to record the memory usage. The good point is that module work on Linux and Mac OSX, also on FreeBSD.

My module is Memory::Stats, so how it works?

use Memory::Stats;
my $mu = Memory::Stats->new;

#Before the stuff you want to track
$mu->start;
#Do your consuming job
$mu->stop;

$mu->report;

You can also add checkpoints:

use Memory::Stats;
my $mu = Memory::Stats->new;

#Before the stuff you want to track
$mu->start;
#Do your consuming job

$mu->checkpoint("before sub1");
#sub1
$mu->checkpoint("after sub1");

#Other stuff

$mu->checkpoint("before sub2");
#sub2
$mu->checkpoint("after sub2");

#Continue your consuming job
$mu->stop;

$mu->report;

Interesting reading:



Share your opinion