Perl's garbage collector uses reference counting to keep track of which data structures are "live" on the heap and which can be deallocated. This means that memory will never be freed if your data structures contain cyclic references, even when all variables referencing those data structures go out of scope. It is generally up to you as a developer to break (or "weaken") such references if you want timely memory deallocation. Otherwise, memory deallocation will happen when your script terminates, but that might be too late, especially in case of long-running processes. (This is a good reason to avoid long-running processes, by the way. Paritioning your code into scripts/processes is a practical example of a fault-tolerance technique known as "micro-reboots".)
To check the overall (maximum) memory footprint of a Perl interpreter, use
valgrind --tool=massif perl yourscript.pl, then
ms_print massif.out.pid. However, note that it slows down the execution significantly and adds even more memory overhead, so handle with care.
Here is a list of developer-oriented Perl modules that will help with troubleshooting memory leaks in Perl:
Devel::Gladiatorshows you counts of all live objects on the heap, by object class/category, including leaked ones. This is somewhat similar to - but more limited than - the jmap tool for Java VMs. You can also get the actual references, which you can then pass to...
Devel::Size, which lets you calculate the amount of memory taken up by a data structure (one that is not yet leaked).
Devel::Cycleexamines a data structure for cyclical references (if such exist, the data structure is bound to cause memory leaks).
Devel::FindRefhelps figure out where references to a data structure are being held.
Devel::Events::Objectsoffers instrumentation for constructor/destructor invocations.
Devel::Leakgives you the total number of currently live data structures.
Finally, you may wish to pass the
-Dm command-line switch to Perl. However, unlike the modules, it requires a version compiled with debugging support and gives low-level information that is difficult to correlate with application-level code.