perl open/close file leaks memory??

J

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 $!;
}
}
 
D

Dr.Ruud

(e-mail address removed) 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;
}
 
M

Mumia W.

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?
 
J

John W. Krahn

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
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top