S
Sinisa Susnjar
Hello Group,
could a Perl Guru please explain to me what is going on here (so I can
become one too ;-) )?!?
Progy1.pl:
#!/usr/bin/perl
while ($line = <>) {
chomp($line);
($key, $value) = split(':', $line);
$myhash{$key} = $value;
}
Progy1.pl shows a small piece of code from a larger program that is
supposed to do mass-data manipulation (>50 mio lines of data).
I was asked to check for memory leaks... it was leaking tons (>4GB) of
memory and I discovered that with rewriting it like Progy2.pl below, the
memory leaks would go away:
Progy2.pl:
#!/usr/bin/perl
use strict;
use warnings;
my %myhash;
while (my $line = <>) {
chomp($line);
my ($key, $value) = split(':', $line);
$myhash{$key} = $value;
}
Yeah, sure - I could lean back and say problem solved, but I would
really like to understand why Progy1 is leaking and Progy2 not...
It seems to me that perl is allocating memory in Progy1.pl for the
variables within the while {} for every single pass through the loop and
never giving them back or reuse or garbage collect them (is the GC too
slow?), while in Progy2.pl - with the variables declared with "my" -
everything works as expected, i.e. no memory leaks, overall memory
consumption aroung 70MB which is ok for the type of files being processed.
btw, I am using perl 5.6.1 on Solaris 2.8...
I read the FAQs (I hope thorougly enough...)
Hope, somebody can help...
Regards,
Sinisa Susnjar
could a Perl Guru please explain to me what is going on here (so I can
become one too ;-) )?!?
Progy1.pl:
#!/usr/bin/perl
while ($line = <>) {
chomp($line);
($key, $value) = split(':', $line);
$myhash{$key} = $value;
}
Progy1.pl shows a small piece of code from a larger program that is
supposed to do mass-data manipulation (>50 mio lines of data).
I was asked to check for memory leaks... it was leaking tons (>4GB) of
memory and I discovered that with rewriting it like Progy2.pl below, the
memory leaks would go away:
Progy2.pl:
#!/usr/bin/perl
use strict;
use warnings;
my %myhash;
while (my $line = <>) {
chomp($line);
my ($key, $value) = split(':', $line);
$myhash{$key} = $value;
}
Yeah, sure - I could lean back and say problem solved, but I would
really like to understand why Progy1 is leaking and Progy2 not...
It seems to me that perl is allocating memory in Progy1.pl for the
variables within the while {} for every single pass through the loop and
never giving them back or reuse or garbage collect them (is the GC too
slow?), while in Progy2.pl - with the variables declared with "my" -
everything works as expected, i.e. no memory leaks, overall memory
consumption aroung 70MB which is ok for the type of files being processed.
btw, I am using perl 5.6.1 on Solaris 2.8...
I read the FAQs (I hope thorougly enough...)
Hope, somebody can help...
Regards,
Sinisa Susnjar