You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Tom Schindl <to...@gmx.at> on 2006/03/29 09:23:01 UTC

Re: No image creation in mod_perl (Or Perl Memory Management is a beast)

And even "worse":

-----------------8<-----------------
#!/usr/bin/perl

use Benchmark;

$t0 = new Benchmark;
&bla();
$t1 = new Benchmark;
# Memory has grown on my machine to 110 MB
#sleep 20;
$t2 = new Benchmark;
&bla();
$t3 = new Benchmark;
# Memory has resides on my machine on 110 MB

print "First run took: " . timestr(timediff($t1, $t0)) . "\n";
print "Second run took: " . timestr(timediff($t3, $t2)) . "\n";

sleep;

sub bla {
   my $var = &blo($ARGV[0]);
#   undef $var;
}

sub blo {
   my $var = "";
      for( 1 .. 20_000_000  ) {
            $var .= "xxx";
      }

      #   undef $var;
      if( ! defined $_[0] ) {
         return $var;
      } else {
         return \$var; ## This will also result in the fact that perl
                       ## releases the memory after the sub has been
                       ## ended
      }
}
-----------------8<-----------------

Just to quote perl gurus from the p5p you'll get to here something like
this:

Yitzchak Scott-Thoennes:
...
Memory is *not* released back to perl.  Lexicals(*) hold on to their
storage space, including the string buffer, under the presumption that
they will be reused.  Perl's memory management strategy can be
described as allocate as early as possible and for as long as
possible.
...

Please note the fact that Lexicals!!! hold their values until they are
undef-ed or they are also GCed when returned as references. You should
also note the time used in the second run (especially sys-time) which is
much lower because memory allocation has not to be done.


>>>Can I confirm that in the Windows implementation, if a thread allocates
>>>requires 100MB of memory and then releases it, the next thread can reuse
>>>that memory?
>>>
>>>Thanks.
>>>
>>>
>>
>>
> 

Because the above said applies to any perl you won't gain much on win32
because memory allocated belongs exactly to the sub it was allocated in ;-)

Tom