Profiling Perl app memory usage

Nik Clayton nik at ngo.org.uk
Thu Feb 2 12:07:31 GMT 2006


Is anyone aware of anything that makes it easier to portably track a Perl
app's memory usage?

I've been fixing http://rt.cpan.org/Ticket/Display.html?id=17359.  Since the
problem was excessive memory use I needed to track down where in the app the
  memory use dramatically increased.

I ended up littering the code with sections like this:

     warn "At point foo\n";
     warn qx{ ps -o rss,vsz $$ }, "\n";

which (on FreeBSD at least) will print out the process' resident and virtual
size at various points.

This works, but is time consuming.  It also means that I can't write a test
for this.  In my ideal world my test suite would contain a 'base line'
memory profile for the application, and future test runs would then be able
to flag up any changes that I've made that significantly alter the
application's memory profile, warning me if I've done something boneheaded
again.  It would also give me a head start by showing me where in the
application memory usage started to grow.

I've figured out how to hook in to the debugger to get arbitrary code run at
each subroutine entry point [1] [2] so that's a solved problem.

What I can't see is how to (portably) determine the program's current memory
footprint.

The ps(1) approach is simple, but non-portable.

I'm aware of Devel::Peek::mstat(), but that needs to run under a Perl
configured with Perl's malloc (which might not be the case on the
'production' Perl, so the memory footprint stats may not be comparable), and
the Perl needs to be built with a specific debugging flag.  I also (and this
may be down to me not groking the docs) am unsure how I'd use ::mstat() to
get the RSS and VSZ figures.

Walking through CPAN modules that match /devel/i and /prof/i I can't see
anything that can reliably extract this information.

Anyone know of anything, or can suggest an approach?

N

[1] http://search.cpan.org/src/JESSE/Devel-CallTrace-1.0/lib/Devel/CallTrace.pm

[2] http://www.perl.com/pub/a/2004/06/25/profiling.html



More information about the london.pm mailing list