I've identified what seems to be a memory leak in Perl and verified it on 5.8.0 on a Windows machine and 5.005_03 on a UNIX server. It seems to occur when an anonymous subroutine is defined with code that would cause recursion. The simplified example below demonstrates the problem. Try running it on your system with Task Manager or `top` running to see the memory usage climb. On my system it gobbles up about 100MB per second for the first 3 seconds. Note that &$sub_ref() is never actually called: the recursion is defined but never executed.
use strict; # leak occurs whether strict is used or not
while(1) {
&leak();
}
sub leak {
my $sub_ref;
$sub_ref = sub { &$sub_ref(); };
return 0;
}
To provide context, a more realistic usage might involve computing a factorial.
sub leak {
my $factorial_sub_ref;
$factorial_sub_ref = sub {
my ($number) = @_;
if ($number < 2) { return 1; }
return($number + &$factorial_sub_ref($number - 1));
};
return 0;
}
Additional context: this problem originally occured for me in a recursive descent parser, which had a whole lot of tiny functions I wanted to obscure from UltraEdit's function list.
Is this a Perl bug? If so, where can I find more information about its status?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.