Y
Yash
Here is an unexpected behavior in Perl:
#!/usr/bin/perl
$line=readline STDIN;
while ($line ne "") {
chomp $line; # remove newline from $line
@mlsFields = split(/,/,$line);
$line=readline STDIN;
}
When I run this program on HP-Ux using
cat myfile.txt | a.pl
and monitor the memory usage using top,
I see the memory continuously increasing from
CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU
COMMAND
0 pts/0 21224 dilbert 237 20 6552K 720K run 0:04 78.36 17.33
perl
to
CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU
COMMAND
1 pts/0 21224 dilbert 227 20 21400K 15492K run 0:49 98.95 90.82
perl
and further.
This implies that the array @mlsfields is repeatedly being allocated
without being cleared. I would ideally expect Perl to overwrite the
existing array during each iteration of the loop.
Changing the line to :
my @mlsFields = split(/,/,$line);
solved the problem. The memory usage remains stable after making this
change.
Can someone explain this?
Also, by making the array my, do you think the array would be
overwritten? or would it be deallocated and reallocated for the new
iteration?
Thanks
#!/usr/bin/perl
$line=readline STDIN;
while ($line ne "") {
chomp $line; # remove newline from $line
@mlsFields = split(/,/,$line);
$line=readline STDIN;
}
When I run this program on HP-Ux using
cat myfile.txt | a.pl
and monitor the memory usage using top,
I see the memory continuously increasing from
CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU
COMMAND
0 pts/0 21224 dilbert 237 20 6552K 720K run 0:04 78.36 17.33
perl
to
CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU
COMMAND
1 pts/0 21224 dilbert 227 20 21400K 15492K run 0:49 98.95 90.82
perl
and further.
This implies that the array @mlsfields is repeatedly being allocated
without being cleared. I would ideally expect Perl to overwrite the
existing array during each iteration of the loop.
Changing the line to :
my @mlsFields = split(/,/,$line);
solved the problem. The memory usage remains stable after making this
change.
Can someone explain this?
Also, by making the array my, do you think the array would be
overwritten? or would it be deallocated and reallocated for the new
iteration?
Thanks