I have a script that has memory leaks. I believe this is because after executing undef on my nested objects, the amount of memory in the script does not change. I used Devel :: Cycle to find any circular links, and I turned these circular links into weak links with Scalar::Util . The problem still remains.
Now I'm trying to use Valgrind to solve the problem. As a first run with valgrind, I checked things in the world program perl hello:
#! /usr/bin/perl use strict; use warnings; print "Hello world!\n";
Here was the output of valgrind when running valgrind --trace-children=yes perl ./hello_world.pl :
==12823== HEAP SUMMARY: ==12823== in use at exit: 290,774 bytes in 2,372 blocks ==12823== total heap usage: 5,159 allocs, 2,787 frees, 478,873 bytes allocated ==12823== ==12823== LEAK SUMMARY: ==12823== definitely lost: 13,981 bytes in 18 blocks ==12823== indirectly lost: 276,793 bytes in 2,354 blocks ==12823== possibly lost: 0 bytes in 0 blocks ==12823== still reachable: 0 bytes in 0 blocks ==12823== suppressed: 0 bytes in 0 blocks ==12823== Rerun with
My understanding, from here , is that when the number of allocs not equal to the number of frees , you have a memory leak.
Since all I do is print hello world, I have to ask the question, does the Perl interpreter itself, here v5.10.1, have at least its own memory leak or am I interpreting everything wrong?
I would like to understand this before tackling my actual perl script.
ADDITION
I see the following in Perl 5.12.0 delta :
Weak reference to a hash leak. This affected the DBI [RT # 56908].
This may eventually apply to my full version of perl script and not to this hello world program, but it makes me think that I need to go through installing the latest version of perl as non-root.
ADDENDUM2
I installed activestate perl 5.16.3 and the problem as well as my actual script problems still remain.
I suspect that in the case of this welcoming world program, I should misuse / interpret valgrind, but I don't understand yet.
Update1 Daxim’s answer really matters. When I enter the following line into my perl script:
use Perl::Destruct::Level level => 1;
Then the output of valgrind:
==29719== HEAP SUMMARY: ==29719== in use at exit: 1,617 bytes in 6 blocks ==29719== total heap usage: 6,499 allocs, 6,493 frees, 585,389 bytes allocated ==29719== ==29719== LEAK SUMMARY: ==29719== definitely lost: 0 bytes in 0 blocks ==29719== indirectly lost: 0 bytes in 0 blocks ==29719== possibly lost: 0 bytes in 0 blocks ==29719== still reachable: 1,617 bytes in 6 blocks ==29719== suppressed: 0 bytes in 0 blocks ==29719== Rerun with
This is a significant difference. My own memory leak problems with my script remain, but at least this welcome global program now seems reasonable to valgrind.
This whole thing, although it asks the question, what is the point of stopping hard circular links from Scalar::Util if memory is not freed before the program exits, prohibiting the use of this somewhat esoteric module Perl::Destruct::Level ???