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

After reading a lengthy __DATA__ section and issuing a "close(<DATA>);" is the memory allocated to the actual data following the __DATA__ marker freed up?

It just occured to me that by reading the data and stuffing it into hashes my module could quite possibly be consuming more memory than if I just defined the hashes with the data explicitly. (..and I don't know how to check that)

thanks!

Replies are listed 'Best First'.
Re: Memory Usage with __DATA__
by Thelonius (Priest) on May 16, 2003 at 02:52 UTC
    The DATA is not held in memory. DATA is just a file descriptor into the script file. It takes no more memory than reading any other file.

      Its interesting to note that the DATA file handle only gets initialized when there an __END__ or __DATA__ tag is present, if they are omitted the filehandle isnt created. Try this:

      use strict; use warnings; while (<DATA>) { print } print "--\n"; seek(DATA,0,0); while (<DATA>) { print } # uncomment the data block for this program to work #__DATA__ #test #test

      which outputs

      readline() on unopened filehandle DATA at D:\perl\scratch\test__data__ +.pl line 3. seek() on unopened filehandle DATA at D:\perl\scratch\test__data__.pl +line 7. readline() on unopened filehandle DATA at D:\perl\scratch\test__data__ +.pl line 8. --

      So presumably whatever memory consumed by the filehandle can be avoided if that makes a difference. I doubt it if it does.


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

      But isn't the script source retained in memory?

        Yah it is, but POD isnt, and nor is anything after the __END__ / __DATA__ tags. When the parser(/lexer) hits one of these tags it stops but doesnt close the file descriptor.


        ---
        demerphq

        <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Memory Usage with __DATA__
by chip (Curate) on May 16, 2003 at 14:25 UTC
    Well, first of all, the script itself doesn't occupy memory per se ... it's a file that the perl binary holds open. So closing the filehandle can't free what isn't allocated.

    On the other hand, closing DATA does free the memory buffer that is associated with the filehandle (just like any filehandle). Can't hurt...

        -- Chip Salzenberg, Free-Floating Agent of Chaos