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

Am just wondering how to write a perl program which can occupy 1GB of memory and release it when i ask for it ?

Replies are listed 'Best First'.
Re: how to occupy 1G of memory
by BrowserUk (Patriarch) on Jul 12, 2010 at 11:13 UTC

    Here's a simple and efficient way:

    #! perl -slw use strict; my $ramfile; open RAM, '>', \$ramfile; seek RAM, 1024**3 - 1, 0; print RAM chr(0); close RAM; printf "Check ram: "; <>; undef $ramfile; printf "Check ram: "; <>; __END__ c:\test>GbRamFile.pl Check ram: 1056792k Check ram: 6168k

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      can u please explain this ?

      specifically, what is the file you open ? and what is that calculation ? ( how it is 1 GB )
        can u please explain this ?

        Was the other anonymonk's explanation enough?

        Its special in-memory file handle, see perldoc -f open

        The calculation is the number 1GB