'my' array variables avoid memory leaks?

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
 
A

A. Sinan Unur

(e-mail address removed) (Yash) wrote in @posting.google.com:
Here is an unexpected behavior in Perl:
#!/usr/bin/perl

use strict;
use warnings;
$line=readline STDIN;
while ($line ne "") {
chomp $line; # remove newline from $line
@mlsFields = split(/,/,$line);
$line=readline STDIN;
}

The code above is not correct:

readline EXPR
Reads from the filehandle whose typeglob is contained in EXPR.
In scalar context, each call reads and returns the next line,
until end-of-file is reached, whereupon the subsequent call
returns undef.

You never check for EOF in your code.

I tend not to care about the internals of how Perl deals with my variables.
It is better to scope your variables to the smallest appropriate scope and
tends to work fine. So, I would rewrite the routine above as

while(<STDIN>) {
chomp;
my @fields = split /,/;
# do something with @fields I presume
}

or, if you really want to use readline,

while( my $line = readline *STDIN) {
chomp $line;
my @fields = split /,/, $line;
# do something with @fields
}

If your memory problem goes away when you properly scope your variables, I
would be inclined to suspect that the code you showed us is not the real
code, and there are some other globals involved etc. not really care to
investigate further, but suggest that you use strict and warnings.
 
B

Brian McCauley

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?

Conecptually it's deallocated and reallocated. Implementationally it
may get overwritten if perl thinks it is safe to do so. But you
shouldn't need to worry about this. From a Perl programmer's point of
view it's deallocated and reallocated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top