I tracked down an apparent memory leak in a program I wrote to the way perl seems to deal with hashes. The program uses some fairly large hashes that need to be completely redefined on a regular basis. I had been using "my" within a block scope thinking that the memory allocated for the hash would be freed (at least for use by perl for later) when the end of the block was reached. This does not seem to be exactly the case. In the first (simplified) example:
{
my %hash;
for my $i (1 .. 500000) {
$hash{$i}=1;
}
}
sleep 1;
{
my %hash;
for my $i (1 .. 500000) {
$hash{$i}=1;
}
}
sleep 1;
{
my %hash;
for my $i (1 .. 500000) {
$hash{$i}=1;
}
}
sleep 1;
...
The amount of memory needed for the hash is 36MB (on my system). After the first block completes though, every other block increases the size of the memory being used by the program by a couple of MBs. However, when I add "undef" statements inside the blocks like so:
{
my %hash;
for my $i (1 .. 500000) {
$hash{$i}=1;
}
undef %hash;
}
sleep 1;
...
this does not occur, the resident memory stays constant at 36 MB. My question is: Why is extra memory being used in the first example but not the second and is the latter scenerio the best way to handle this? I have tried this on Linux and SCO UNIX with perl 5.8 and 5.005_03 respectively, I think this is a perl thing.
Any insight is greatly appreciated.
Thanks,
tigervamp
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.