On Thu, 7 Dec 2000, Ivan E. Panchenko wrote:

> 
> 
> Today I discovered a strange behaiviour of perl, 
> and I wonder if anybody can tell me what to do with it.
> 
> The matter is that perl DOES NOT REUSE MEMORY allocated for 
> intermediate calculation results. This is specially harmful to
> data-intensive modperl applications where one perl process processes
> many queries and can leak great amount of memory.

When you will rerun the same code again (the same request) in the same
httpd process, the memory won't grow. There is no leak, that's the way
Perl optimizes memory allocation. 

On the contrary, when you will rerun the same code in the same process,
it'll be executed even faster since the memory allocation for lexically
scoped variables $a and $b was done in the first execution. So even if you
undef them the memory is still allocated.

You can easily test it with httpd -X. See more examples of this kind of
testing with gtop and Devel::Peek in the performance chapter in the guide.

Of course if you don't like it, you can complain to p5p list :)

> The example is below:
> 
> use BSD::Resource; 
> my $cc = 'a' x 20000000 ;        # alocates 20Mb for the right part and
>                                # 20Mb for $a
> &p;
> { my $a = $cc.'x';             # allocates 20 more Mb for right part
>                                # and 20 for a
> &p;
>   undef $a;                    # deallocates $a
> }
> &p;
> { my $b = $cc.'y';             # allocates 20 more Mb for right part
>                                # and reuses deallocated 20Mb for b 
>  &p;
>   undef $b; 
> }
> &p;
> 
> sub p { 
>   print STDERR "used memory = ".(BSD::Resource::getrusage)[2]."\n"
> }
> 
> # end of example.
> Output:
> used memory = 40772
> used memory = 79804
> used memory = 80068
> used memory = 99676
> used memory = 99700
> ##
> Here I used BSD:Resource to measure consumed memory. Its result seems to
> be correlated with the amount of memory taken by the process from the OS.
> #
> 
> This was checked on FreeBSD 3.4 and 4.2 ; and perl5.00405 5.00503 .
> Same things where noticed on Linux and probably on Solaris too.
> 
>               Ivan
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 



_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/  



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to