You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Andrew Wyllie <wy...@dilex.net> on 2000/12/07 20:58:54 UTC

[OT] Re: perl's memory leak

Hi Ivan,

It's not really mod_perl, but is relevant to people on the list I guess...

If you really play aorund with this, you'll find some interesting variations.
If I assign $cc using a for loop

	my $c;
	for ( 1..20000000) { $cc .= 'a'; } 

	it's a lot slower, but only uses half as much ram

also if I do the assignment first and then add a char:

	my $a = $cc;       # allocates 20 more Mb for right part          
	p( "assigned a" );
	
	$a .= 'x';
	p( "changed a" );
	
	undef $a;          # deallocates $a 
	p( "undefed a" );
	
	my $b = $cc;       # allocates 20 more Mb for right part
	$b .= 'a';
	                   # and reuses deallocated 20Mb for b
	p( "defined b" );
	undef $b;
	p( "undefed b");
	
	
	sub p {
	        my $mesg = shift;
	  print "$mesg used memory = ".(BSD::Resource::getrusage)[2]."\n"
	}

I get:

	assigned a used memory = 40212
	changed a used memory = 40212
	undefed a used memory = 40212
	defined b used memory = 40712
	undefed b used memory = 40712

Which is more what you would hope to see right?


andrew


On Thu, 07 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.
> 
> 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: modperl-unsubscribe@apache.org
> For additional commands, e-mail: modperl-help@apache.org

...............................................................................
Andrew  Wyllie       <wy...@omasum.com>            Open Source Integrator
v.206.729.7439              __We can catify or stringify,
c.206.851.9876                    separately or together!__ perl-5.005_03