I'm sorry if I haven't been clear before; I'll try to explain it with more words this time.

Let's say you have 1G of memory; you build a huge data structure in a hash; for that perl allocates 300M of memory from the operating system.

You store that hash in a lexical variable my %h = .... When this variable goes out of scope, perl internally marks the memory as "not needed anymore". However it doesn't give the memory back to the OS, because it suspects that you might use more memory later on in your program.

Now if you wait for 30s, perl decides that it can give the memory back, because apparently you don't need it anymore.

On the other hand if you don't wait, but allocate another huge data structure, perl will use the memory that %h occupied before, and only when that is exhausted it will ask the operating system for more memory.

So if your program has the form

* use up 300M of RAM * let the variables go out of scope that point to the memory * build a data structure that needs 400M

then the last step only increases the memory usage of the perl script by 100M, because it uses the 300M first that was marked as not-needed-anymore.

This is why I wrote that you shouldn't bother to sleep before doing the second task - perl will not waste your memory, unless you do something stupid (like keep a reference to the first, huge hash).


In reply to Re^3: wait till memory is flushed by moritz
in thread wait till memory is flushed by Boetsie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.