Memory leak or expected behavior?

syost_at_quicktopic.com
Date: 12/30/04


Date: 29 Dec 2004 22:51:23 -0800

I've always seen my Apache/mod_perl httpds grow surprisingly large in
memory, and I want to fix that. But I'm seeing standalone Perl behavior
that could account for it. It seems that a lexical variable's allocated
memory isn't getting reused at all when it goes out of scope and
returns. I wrote the following code to demonstrate:

#!/usr/bin/perl
use strict;
use BSD::Resource;

foreach my $i (1..5) {
print_rusage("Before scope_it sub ");
scope_it();
print_rusage("After scope_it sub ");
print "\n";
}

sub scope_it {
print_rusage("Before instantiation");
my $tm = TestMem->new();
$tm->alloc();
print_rusage("After instantiation");
}

sub print_rusage {
my $message = shift;
my $pid = $$;
my ($usertime, $systemtime,
$maxrss, $ixrss, $idrss, $isrss, $minflt, $majflt, $nswap,
$inblock, $oublock, $msgsnd, $msgrcv,
$nsignals, $nvcsw, $nivcsw) = BSD::Resource::getrusage();
print "$message: ixrss:$ixrss; idrss:$idrss; isrss:$isrss\n"
}

package TestMem;

sub new {
return bless {};
}

sub alloc {
my $me = shift;
my @bigbuf = (1..1000000); # In real life the array is read from a
file
$me->{mem} = \@bigbuf;
}

sub DESTROY {
my $me = shift;
print STDERR "Destroying " . ref($me) . "\n";
}

Running this prints the following, where the idrss (integral or current
unshared data) number grows each time. Any idea what's happening and
how I can change my code to prevent it?

Thanks,
Steve Yost

Before scope_it sub : ixrss:48544; idrss:2058712; isrss:9472
Before instantiation: ixrss:48544; idrss:2058712; isrss:9472
After instantiation: ixrss:64288; idrss:3318740; isrss:12544
Destroying TestMem
After scope_it sub : ixrss:68880; idrss:3687388; isrss:13440

Before scope_it sub : ixrss:68880; idrss:3687388; isrss:13440
Before instantiation: ixrss:68880; idrss:3687388; isrss:13440
After instantiation: ixrss:84624; idrss:4948952; isrss:16512
Destroying TestMem
After scope_it sub : ixrss:89216; idrss:5318048; isrss:17408

Before scope_it sub : ixrss:89216; idrss:5318048; isrss:17408
Before instantiation: ixrss:89216; idrss:5318048; isrss:17408
After instantiation: ixrss:104960; idrss:6579612; isrss:20480
Destroying TestMem
After scope_it sub : ixrss:110208; idrss:7001436; isrss:21504

Before scope_it sub : ixrss:110208; idrss:7001436; isrss:21504
Before instantiation: ixrss:110208; idrss:7001436; isrss:21504
After instantiation: ixrss:125952; idrss:8263000; isrss:24576
Destroying TestMem
After scope_it sub : ixrss:130544; idrss:8632096; isrss:25472

Before scope_it sub : ixrss:130544; idrss:8632096; isrss:25472
Before instantiation: ixrss:130544; idrss:8632096; isrss:25472
After instantiation: ixrss:146288; idrss:9893660; isrss:28544
Destroying TestMem
After scope_it sub : ixrss:150880; idrss:10262756; isrss:29440



Relevant Pages

  • sitemap generator for Perl
    ... I want to run the sitemap generator ... Returns the minimum number of links to traverse from the root URL of ... my $class = shift; ...
    (perl.beginners)
  • Re: How can I create instantiable objects (not classes)?
    ... a child object inherits not only its parent object's ... sub fee { ... my $class = shift; ... For example, an object of type Car might receive a message named "ticket," and since a car does not know what to do with a ticket, it would pass that message to an object of type Driver. ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Memory Problem
    ... > I am using a 64MB device -- and have been pinvoking GlobalMemoryStatusCE ... > ** % Load from Memory Load returned from GlobalMemoryStatusCE ... > Each time I see the 5MB array allocation reflected properly. ... >>> Public Sub New ...
    (microsoft.public.dotnet.framework.compactframework)