bsb has asked for the wisdom of the Perl Monks concerning the following question:

After considerable effort I discovered what seems like a leak caused by repeatedly "do"ing n file that reads from it's DATA handle. Closing it stops the leak.

Is this a known problem?

$ perl -nwe 'BEGIN {print "$$\n"} do "file_with_DATA.pl"' $ head -n 10 file_with_DATA.pl () = <DATA>; #close(DATA); 1; __DATA__ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA [... Many more lines of "AAAA"s ...]
Update:
p5pmsg Potential to leak DATA filehandles
man perldata:
The filehandle is left open pointing to the contents after __DATA__. It is the program's responsibility to "close DATA" when it is done reading from it.

Replies are listed 'Best First'.
Re: do + <DATA> = leak
by simonm (Vicar) on Dec 03, 2003 at 03:46 UTC
    Yup, I get a similar effect in Perl v5.6.0 built for darwin -- it leaks unless there's an explicit close().

    In fact, if you let the test script run long enough, it eventually runs into some other kind of problem on repetition number 252; perhaps it's running out of file handles? perl -w -l -e 'BEGIN {print "$$\n"} while ( 1 ) { do "file_with_DATA.pl"; print( ( ++ $i ) . ": " . `date`, ` ps -o rssize $$` ) }'

Re: do + <DATA> = leak
by Roger (Parson) on Dec 03, 2003 at 02:20 UTC
    Don't know what sort of leak you are talking about. When I tried to run it, it just waits for me to hit the return key.

    perl -h explains that the -n switch assume while (<>) { ... } loop around program, so it is waiting for input from STDIN.

    So I kept hitting <ENTER>, and watched the memory usage of perl on another terminal. The memory usage seemed quite stable at 4224k. Note that I did not have the close(DATA) line in my test script.

      My real file has a lot more lines of "AAAAA"s.

      It does seem to leak for me:

      $ yes|perl -nwe 'BEGIN {print "$$\n"} do "file_with_DATA.pl"; print `p +s -o rssize $$`' 26279 RSS 1120 RSS 1128 RSS 1132 [..etc..] RSS 5564 RSS 5572 RSS 5576
      This is perl, v5.6.1 built for i386-linux
        Ah... I see it. I am using perl 5.6.1 for sun-solaris. The memory leak remains even after I add the close(DATA);