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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to