RE: Memory leak with shared memory

[email protected] ("Jerry D. Hedden")
Newsgroups perl.ithreads
Message-ID <20060510120331.fb30e530d17747c2b054d625b8945d88.b7bd7c695d.wbe@email.secureserver.net>
Adrien wrote:
> I have a "big" problem with threads and shared memory.
> 
> Example :
> #!/usr/bin/perl
> 
> use threads;
> use threads::shared;
> 
> my $char_test : shared;
> $char_test = "A";
> 
> for (my $i = 0; $i < 25; $i++)
> {
>     $char_test .= $char_test;
> }
> 
> print "done\n";
> 
> sleep 5;
> 
> print "undef\n";
> undef($char_test);
> print "undef done\n";
> 
> sleep 60;
> 
> If I comment "use threads;", all is ok (memory usage is about 3.5% and
> after undef, about 0.1%). But, if I uncomment "use threads;", memory usage
> of perl is double (7%) and after undef, memory usage is 3.5%...
> You can see that with "ps auxw".
> 
> It's a big problem for my project. Somebody knows this problem?

First of all, you didn't tell us which versions of Perl, 'threads' and
'threads::shared' you're using.

Barring that, I do not see a definite indication of a memory leak.  Perl
uses its own memory management scheme, holding onto memory from the OS
for its own purposes.  'undef'ing a Perl variable does not mean that
the memory it used is returned to the OS - Perl may hang onto it for
later reuse.

To see if there is a 'real' memory leak, you need to run your string
building test inside a loop several times to see if memory usage keeps
going up with each interation, as so:

#!/usr/bin/perl

use strict;
use warnings;
$| = 1;

use threads;
use threads::shared;

my $char_test :shared;

for (1..5) {
    $char_test = 'A';
    for (1..25) {
        $char_test .= $char_test;
    }
    print("Check memory\n");
    sleep(5);
    undef($char_test);
}
print("Check memory - final\n");
sleep(5);

print("Done\n");

When I run this, I see that memory usage is constant and not continually
increasing with each loop.  Therefore, as far as I can tell, there is no
memory leak.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.