Skip to content

Instantly share code, notes, and snippets.

@jkeroes
Created September 21, 2012 20:28
Show Gist options
  • Save jkeroes/3763713 to your computer and use it in GitHub Desktop.
Save jkeroes/3763713 to your computer and use it in GitHub Desktop.
Perl memory leaks resources
Tracking memory leaks:
Devel::Arena - sv_stats() returns arena structures used for SV allocation
* Devel::Cycle - find_cycle($ref) returns all cycles found in $ref and the perl variables they point to
Devel::Gladiator - walk Perl variable arena
Devel::Leak - deprecated by Devel::LeakTrace::Fast
Devel::LeakTrace - deprecated by Devel::LeakTrace::Fast
* Devel::LeakTrace::Fast - prints leaked SV's and line numbers at END.
Data::Structure::Util - has_circular_ref($ref) returns ref to link in $ref that is circular or false.
Test::LeakTrace - no_leaks_ok(), etc.
Test::Memory::Cycle - Based on Devel::Cycle, offers memory_cycle_ok(), etc.
Tracking XS memory leaks:
Devel::Peek - Core module that dumps perlguts info. The examples section has useful perlguts explanations.
Devel::Plumber - use gdb to find and report leaks
Devel::Valgrind::Client - related to Test::Valgrind.
Test::Valgrind - just tracks Newx and malloc-like, not perl objects.
Memory allocation size:
Devel::Size - print size with and without ref traversals.
* Devel::Size::Report - print full report of every ref in a var.
Object-only:
Devel::Events::Handler::ObjectTracker - track every blessed obj created or destroyed via object_bless() and object_destroy()
Devel::Leak::Object - track select or every blessed obj
Devel::LeakGuard::Object - derived from Devel::Leak::Object; adds leakguard { ... } closure.
Fixing memory leaks:
* Data::Decycle - can also decycle coderefs! weaken_deeply(), decycle_deeply(), recsub { ... } - leak free closures
Data::Structure::Util - circular_off($ref) - traverse $ref for cyclical refs and weaken them.
* Object::Destroyer - create sentries for objects and inform them what to do during destruction
Scalar::Util - weaken()
Sub::Recursive - anonymous memory leak free subs. mutually_recursive(), recursive()
Articles on leaks:
How to use Object::Destroyer: http://www.perl.com/pub/2007/06/07/better-code-through-destruction.html
PM leak article collection: http://www.perlmonks.org/?node_id=336883
http://www.perlmonks.org/?node_id=419754
http://perldoc.perl.org/perldebguts.html#Debugging-Perl-memory-usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment