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