pjf has asked for the wisdom of the Perl Monks concerning the following question:

Good day wise monks. I've had the same question asked of me twice in one day, and I've been shamed by the little knowledge and advice I could provide. In order to correct this state of ignorance, I'm hoping to gather any hints and tips that others may have discovered on the matter of memory profiling in Perl.

Perl comes with some excellent time code profiling tools. Devel::DProf is a simple and effective solution to find what subroutines and statements are using the most time. However profiling memory is another matter. Devel::Profile is an excellent way to discover which parts of one's code is being executed, and which are not.

Unfortunately, profiling memory is a difficult task. Code is simple to understand: it executes one line at a time, and is broken up into a larger chunks called subroutines. A regular profiler times each line, or each subroutine, and then a post-processing tool provides a useful summary at the end.

Memory is a different beast. Variables will be shared between multiple lines of code, and built into large structures that may be referenced from multiple locations. These can grow and shrink during a program execution, and not all variables will have names: many are nameless, and only accessed using references.

Perl has Dan Sugalski's Devel::Size, which appears to do an excellent job of reporting memory usage of individual variables as well as more complex data structures. It's good for when one suspects a certain variable is getting a little too large, but does not provide a way to gain a snapshot into Perl's overall memory usage.

Does anyone have any experiences, good or bad, with memory profiling tools for Perl? I'm not after hints to reduce memory usage, but instead tools or processes to gain an insight into the memory usage of a running Perl program.

Many thanks,