perl open/close file leaks memory??

Discussion in 'Perl Misc' started by jimlee2004@gmail.com, Sep 27, 2007.

  1. Guest

    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 $!;
    }
    }
     
    , Sep 27, 2007
    #1
    1. Advertising

  2. Dr.Ruud Guest

    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."
     
    Dr.Ruud, Sep 27, 2007
    #2
    1. Advertising

  3. Mumia W. Guest

    On 09/27/2007 12:30 AM, 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?
     
    Mumia W., Sep 27, 2007
    #3
  4. 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
     
    John W. Krahn, Sep 27, 2007
    #4
  5. 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^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dani
    Replies:
    3
    Views:
    488
  2. Iñaki Baz Castillo
    Replies:
    7
    Views:
    874
    Iñaki Baz Castillo
    Jan 12, 2010
  3. Jerome Sui

    detecting memory leaks in Perl

    Jerome Sui, Jul 13, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    134
    Jerome Sui
    Jul 13, 2003
  4. Iulian Ilea
    Replies:
    1
    Views:
    320
    pcx99
    Dec 21, 2006
  5. Replies:
    4
    Views:
    128
Loading...

Share This Page