Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How do I free memory allocated to an array

by Zombie sushilc (Initiate)
on Jul 09, 2002 at 13:47 UTC ( #180469=categorized question: print w/replies, xml ) Need Help??
Contributed by Zombie sushilc on Jul 09, 2002 at 13:47 UTC
Q&A  > arrays


I have an array of large size. Even though I am using undef to make it completely empty, it still doesnt release memory till the time program exits. Any hints?

Answer: How do I free memory allocated to an array
contributed by manav


perldoc -q "program shrinks"

specifically the part that says

You can't. On most operating systems, memory allocated to a program can never be returned to the system. That's why long-running programs sometimes re-exec themselves. Some operating systems (notably, FreeBSD and Linux) allegedly reclaim large chunks of memory that is no longer used, but it doesn't appear to happen with Perl (yet). The Mac appears to be the only platform that will reliably (albeit, slowly) return memory to the OS.


Answer: How do I free memory allocated to an array
contributed by Abigail-II

Get a different OS? In most OSses, programs never release memory back to the OS - even if they would want to, the OS isn't wired for that.


Answer: How do I free memory allocated to an array
contributed by DentArthurDent

Given Abigail's answer above, it would then be wise to try to exit the script if that is possible. The task that requires the large array could be factored out into a script that could be run separately from within a larger script with fork or backticks or system or whatever. The tricky part would be if the task required a large amount of data to be passed to it. If the task required creating a large array and generating statistics on it or some such thing, then returning a comparitively small amount of data, then the benefit of keeping that a separate script could be large.

Answer: How do I free memory allocated to an array
contributed by Sewi

You should look at the two types of "free" memory:
1. Memory returned to the operating system for re-allocation to (other) processes.
2. Memory returned to the internal "free memory" pool of the process.

Even if you may not be able to return memory to the operating system, you may be able to re-use it within your process. Try the following sample:

sub S { system "ps u $$"; } &S; my $X = "x"x10240000; &S; undef $X; &S; $X = "x"x10240000; &S; undef $X; &S;
You'll see that perl really returns (parts of) the memory used for $X after is has been destroyed. (I didn't expect this, too :-) ).
Answer: How do I free memory allocated to an array
contributed by rjimlad

I'm not sure how quick perl's garbage collection is with this kind of thing - personally I'd set it to an empty array (ie, @foo=() ). Possibly treating it as an arrayref may get more direct results:

...or maybe not. Of course even freed memory is not necessarily not marked as allocated (if you see what I mean).

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (3)
    As of 2018-07-21 16:03 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (449 votes). Check out past polls.