tags:

views:

318

answers:

1

Why does this leak in Perl ?

$ perl -MDevel::LeakTrace::Fast -e 'our @a=(1);our @b=(1)'
leaked SV(0x0x84e053c) from -e line 1

$ perl -v
This is perl, v5.8.0 built for i386-linux-thread-multi
[...]

$ uname -a
Linux ant1 2.4.21-20.ELsmp #1 SMP Wed Aug 18 20:46:40 EDT 2004 i686 i686 i386 GNU/Linux

Thanks!

+10  A: 

It doesn't. Put it in a loop and see for yourself: process memory does not increase with each loop iteration.

for(1 .. 10_000_000)
{
  our @a = (1);
  our @b = (1);
}

All that "leak" message is (probably) telling you is that the program exited with outstanding variables. To silence that message, undef the variables before the end of the program:

perl -MDevel::LeakTrace::Fast -e 'our @a = (1); our @b = (1); undef @a; undef @b;'

FWIW, the same message is printed under perl 5.10.0, so I'm not sure upgrading perl is the answer.

As for why the message appears in some cases but not in others, it's probably a vagary of Devel::LeakTrace::Fast. Many Perl leak detection modules have "idiosyncrasies" (putting it kindly) like this.

Bottom line: the only 100% reliable way to test for a leak is to see if memory usage increases with repeated execution of the code in question. When in doubt, fall back to that.

The various Devel::* modules can help, however. But first it's best to isolate the leaking code: disable half your code and see if the leak persists. Now you have narrowed your search by half. Repeat a few times and you'll quickly get down to the function or statement(s) that leak. Then see if that code still leaks when put in a separate script. Finally, to see why it's leaking, employ the Devel:: modules (e.g., checking for memory cycles with Devel::Cycle).

John Siracusa
ok but why does perl -MDevel::LeakTrace::Fast -e 'our @a = (1);' show no leaks ? but perl -MDevel::LeakTrace::Fast -e 'our @a = (1);our @b = (1);' ?
Pat
Thanks John, problem is I do have a leak in my program and it seems Devel::LeakTrace::Fast is not going to be helpful since it it showing many false positives like this.
Pat