Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I free memory allocated to an array

by sushilc (Initiate)
on Jul 09, 2002 at 13:47 UTC ( [id://180469]=perlquestion: print w/replies, xml ) Need Help??

sushilc has asked for the wisdom of the Perl Monks concerning the following question: (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?

Originally posted as a Categorized Question.

  • Comment on How do I free memory allocated to an array

Replies are listed 'Best First'.
Re: How do I free memory allocated to an array
by manav (Scribe) on Mar 01, 2005 at 17:54 UTC
    see

    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.

    Manav
Re: How do I free memory allocated to an array
by Abigail-II (Bishop) on Jul 09, 2002 at 13:57 UTC
    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.

    Abigail

Re: How do I free memory allocated to an array
by DentArthurDent (Monk) on Dec 09, 2004 at 17:04 UTC
    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.
Re: How do I free memory allocated to an array
by Sewi (Friar) on Apr 22, 2009 at 12:28 UTC
    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 :-) ).
Re: How do I free memory allocated to an array
by rjimlad (Acolyte) on Jul 19, 2002 at 20:35 UTC
    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:
    \@foo=[];
    ...or maybe not. Of course even freed memory is not necessarily not marked as allocated (if you see what I mean).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://180469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found