Are there any tools for finding memory leaks in my

2019-01-13 22:26发布

问题:

I am using ActiveState Perl 5.6 on a Windows 2003 Server, and am having some memory leak issues. Are there any good tools (or even bad tools which would give a clue) which I would be able to use to help find them.

回答1:

All perl program memory leaks will either be an XS holding onto a reference, or a circular data structure. Devel::Cycle is a great tool for finding circular references, if you know what structures are likely to contain the loops. Devel::Peek can be used to find objects with a higher-than-expected reference count.

If you don't know where else to look, Devel::LeakTrace::Fast could be a good first place, but you'll need a perl built for debugging.

If you suspect the leak is inside XS-space, it's much harder, and Valgrind will probably be your best bet. Test::Valgrind may help you lower the amount of code you need to search, but this won't work on Windows, so you'd have to port (at least the leaky portion) to Linux in order to do this.



回答2:

Devel::Gladiator will show you a list of how many of each variable type Perl has in memory at any given time, and what they are references to. Very useful for figuring out what type of objects are being created but not freed.



回答3:

Since it's not been mentioned yet, Devel::Size will report the size of a data structure. There's no other information given and the rules it uses to determine the 'boundary' of your data structure are opaque. For simple structures this isn't a problem.

Devel::SizeMe is a hobby project of mine that aims to resolve the problems of Devel::Size and enable visualization of the entire memory usage of a perl interpreter. See my blog for extra information, including links to screencasts and videos. One of my goals is to enable detection and visualization of leaks, but that's still a way off yet.

Updates:

In addition to the other comments, you may find my Perl Memory Use talk at LPW2013 useful. I'd recommend watching the screencast as it explains the slides and has some cute visuals and some Q&A at the end.

I'd also suggest looking at Paul Evans Devel::MAT module which I mention in the talk.