Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Another memory usage question

by TheMarty (Acolyte)
on Oct 06, 2004 at 10:41 UTC ( #396934=perlquestion: print w/replies, xml ) Need Help??

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

Related to my earlier problem. Just - how it would be possible to free memory from within Perl script? I tried to do @Array = undef, or @Array = (), but it does not seem to work (the memory is still occupied)... Is there any command/extra module which does something like "flush" of not used structures?

Replies are listed 'Best First'.
Re: Another memory usage question
by PodMaster (Abbot) on Oct 06, 2004 at 10:48 UTC
      Thanx. In simple words -> I should write my 33000 lines of code once again... And regarding search -> I tried "memory" and "memory usage" but it gives completely different effects than "free memory". Funny it is - the search at Perl Monks.
Re: Another memory usage question
by borisz (Canon) on Oct 06, 2004 at 10:45 UTC
    Unless you have a circular list, the memory is freed, but freed to perl not to your operating system. This means perl reuse the memory.
Re: Another memory usage question
by Happy-the-monk (Canon) on Oct 06, 2004 at 17:28 UTC

    Having had your problem just one week ago:
    production software consuming memory allocated for an array inside a for loop,
    I've searched PM and read the threads on it.

    Do a search on "undef array" to see what I (might) have found then.

    In short and as a rule of thumb:

    • @array = (); # free the memory allocated for the contents of the array. Frees less than 90% of the memory.
    • undef @array; # as above and free the memory used for the index of the array. Frees nearly 100% of it.

    I was also asked to test on different behaviour in past Perl versions when freeing memory in arrays. I didn't encounter anything funny from 5.0005_04 through the 5.6es towards 5.8.5.

    I used Devel::Size for the perl data and unix's ps command to look at the overall size of the process. The latter produced nothing unexpected: As the array grows, so does the size of the programme in the perl interpreter.
    You can't return the used memory to the OS, if that's what you are looking for.
    If that's your concern, consider forking a child that will later consume that much memory, and die, freeing the memory then.

    Cheers, Sören

      Thanx. I put "use strict" (YEEES, I KNOW, I've should done this from very beginning), and than I did undef @Array everywhere (almost) where possible. It helped a little bit. 50-60% mem usage less...
Re: Another memory usage question
by Ven'Tatsu (Deacon) on Oct 06, 2004 at 15:37 UTC
    It is my experiance that undef($var) and $var = undef behave quite differently with regards to memory being freed. The former seems to have a much more desirable effect for memory management.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://396934]
Approved by Paulster2
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2019-11-18 11:29 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (89 votes). Check out past polls.