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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
According to the Learning Perl book, truncating an array does not recover its memory. To feee memory I need to undef the array like this undef(@array). It also says that the memory doesn't always get free because not many operating systems support this. I find this strange as I've never had this problem in other languages.

I'm using Win2K and not much memory gets free when I undef my array. When I run the script below the @array takes ~76,000K in memory when I hit the breakpoint. After undef(@array) gets executed the memory only goes down to ~72,000K. If I continue the loop and watch memory it'll fluctuate between these two numbers.

Is there another way to free the memory? Does anyone else find this behavior strange? I must be missing something, perhaps some monks can enlighten me.

Thanks.

use strict; use warnings; print $$; my @array; my $i = 0; while (1) { $array[$i++]= '1234567890'; if ($i == 1_000_000) { undef(@array); #BREAKPOINT HERE $i = 0; } }
UPDATE
The memory is even kept after the array goes out of scope.
use strict; use warnings; print "PID: $$\n"; { my @array; my $i = 0; while ($i <= 1_000_000) { $array[$i++]= '1234567890';# for([0..1_000_000]); if ($i == 1_000_000) { undef(@array); } } } print "END\n"; # BREAKPOINT HERE, MEMORY IS STILL NOT FREE

In reply to Freeing memory used by arrays by monktim

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-20 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found