perl open/close file leaks memory??
perl open/close file leaks memory??
am 27.09.2007 07:30:04 von jimlee2004
I have a test script below.
It seems that there is memory leaks but all this script does is simply
open and close file handle,
Could somebody explain to me what is happening?
use strict;
use FileHandle;
open MY_HANDLE,">>","/tmp/test/testlog.1" or die $!;
my $a=0;
my $b=0;
while(1){
my $mystr="Something";
print MY_HANDLE $mystr;
$a++;
if ($a>1000)
{
$a=0;
close MY_HANDLE or die $!;
system("rm /tmp/test/testlog.$b");
$b++;
open MY_HANDLE,">>","/tmp/test/whatever.$b" or die $!;
}
}
Re: perl open/close file leaks memory??
am 27.09.2007 08:51:30 von rvtol+news
jimlee2004@gmail.com schreef:
> I have a test script below.
> It seems that there is memory leaks but all this script does is simply
> open and close file handle,
No, it doesn't. For one, your $b is out of sync. Further,
s/whatever/testlog/. And I don't see why you have "use FileHandle;".
> Could somebody explain to me what is happening?
>
> use strict;
> use FileHandle;
> open MY_HANDLE,">>","/tmp/test/testlog.1" or die $!;
> my $a=0;
> my $b=0;
>
> while(1){
> my $mystr="Something";
> print MY_HANDLE $mystr;
> $a++;
> if ($a>1000)
> {
> $a=0;
> close MY_HANDLE or die $!;
> system("rm /tmp/test/testlog.$b");
> $b++;
> open MY_HANDLE,">>","/tmp/test/whatever.$b" or die $!;
> }
> }
#!/usr/bin/perl
use strict;
use warnings;
my $pre = "/tmp/test/testlog";
my $ext = 1;
my $fname; # "$pre.$ext"
my $fh;
my $i = 0;
my $max = 1000;
my $str = "Something ";
while(1){
if (0 == $i) {
$fname = "$pre.$ext";
open $fh, ">>", $fname
or die "Error opening '$fname': $!";
}
print $fh $str;
++ $i <= $max and next;
close $fh
or die "Error closing '$fname': $!";
unlink($fname)
or die "Error deleting '$fname': $!";
$i = 0;
++ $ext;
}
--
Affijn, Ruud
"Gewoon is een tijger."
Re: perl open/close file leaks memory??
am 27.09.2007 08:59:41 von paduille.4061.mumia.w+nospam
On 09/27/2007 12:30 AM, jimlee2004@gmail.com wrote:
> I have a test script below.
> It seems that there is memory leaks but all this script does is simply
> open and close file handle,
> Could somebody explain to me what is happening?
>
> use strict;
> use FileHandle;
> open MY_HANDLE,">>","/tmp/test/testlog.1" or die $!;
> my $a=0;
> my $b=0;
>
> while(1){
> my $mystr="Something";
> print MY_HANDLE $mystr;
> $a++;
> if ($a>1000)
> {
> $a=0;
> close MY_HANDLE or die $!;
> system("rm /tmp/test/testlog.$b");
> $b++;
> open MY_HANDLE,">>","/tmp/test/whatever.$b" or die $!;
> }
> }
>
I don't see a memory leak with your program. However, your program does
eventually use all of the memory available to the /tmp filesystem.
On line 4 you open /tmp/test/testlog.1, but on line 19 you open
/tmp/test/whatever.$b. Was that intentional?
Re: perl open/close file leaks memory??
am 27.09.2007 09:14:12 von Dummy
jimlee2004@gmail.com wrote:
> I have a test script below.
> It seems that there is memory leaks but all this script does is simply
> open and close file handle,
> Could somebody explain to me what is happening?
>
> use strict;
And don't forget:
use warnings;
> use FileHandle;
You are not using this module?
> open MY_HANDLE,">>","/tmp/test/testlog.1" or die $!;
> my $a=0;
> my $b=0;
>
> while(1){
This is an infinite loop. That means that it will continue running until you
run out disk space.
> my $mystr="Something";
> print MY_HANDLE $mystr;
> $a++;
> if ($a>1000)
> {
> $a=0;
> close MY_HANDLE or die $!;
> system("rm /tmp/test/testlog.$b");
Why not use perl's built-in unlink() function?
> $b++;
> open MY_HANDLE,">>","/tmp/test/whatever.$b" or die $!;
You are creating new files that will never be deleted.
"/tmp/test/testlog.$b" ne "/tmp/test/whatever.$b"
> }
> }
>
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: perl open/close file leaks memory??
am 27.09.2007 11:46:51 von Michele Dondi
On Thu, 27 Sep 2007 07:14:12 GMT, "John W. Krahn"
wrote:
>> open MY_HANDLE,">>","/tmp/test/whatever.$b" or die $!;
>
>You are creating new files that will never be deleted.
Which are in /tmp which maybe is really held in RAM.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,