简体   繁体   中英

Perl memory leak in threads (threads don't release memory)

I am now searching for 2 weeks about this problem and seems that I can't find any answer just searching on the web.

So, here is two piece of code:

1:

#! /usr/bin/perl

#
# RELEASE MEMORY
#

use threads;

my @child;

$ii=0;


while (1)
{
  my @child = threads->new(\&test, "test");
  $_->detach for @child;
  print "$ii\n";
  $ii++;
}

sub test {
    my ($ee) = 0;    
}

2:

#! /usr/bin/perl

#
# DO NOT RELEASE MEMORY
#

use threads;

my @child;

$ii=0;


for($ii=0;$ii<2000;$ii++) {
    my @child = threads->new(\&test, "test");
    $_->detach for @child;
    print "$ii\n";
}

while(1)
{
  sleep(10);
}

sub test {
    my ($ee) = 0;    
}

So here's the problem. The first code run only one infinite loop and it release memory back to operating system about each 2 seconds (according to ps)

The second code is releasing memory too but only when he's running inside the "for" loop. Once it exit the for loop and enter the infinite loop all the memory that have not been freed into the for loop is never released back to the operating system.

Is anybody experiencing the same issue ?

Perl: (v5.16.1) built for x86_64-linux-thread-multi

OS: Debian 6.0.5

Thanks a lot


Edit 1: I used 800 threads and all verified that they exit by printing the $ee var.

But once entering the while(1) loop here's the ps aux | grep perl output:

root@srv:~# ps aux | grep perl
root      6807 41.5  2.5 387780 209580 pts/0   S+   16:38   0:02 /opt/ActivePerl-5.16/bin/perl /home/tttlast.pl
root      7627  0.0  0.0   7548   856 pts/1    S+   16:38   0:00 grep perl

So all thread exited but the memory usage is still 2.5% of my server total memory. So unless I kill the program the memory is still in use.


Edit 2:

I resolved my problem, I changed my structure. In fact the main program (the long running one) have been separated and I use small program who wait before all threads finish to exit.

This way It doesn't full the virtual memory and other daemon are not killed.

Thanks to everyone leading me to that solution.

There is no "issue" here. This is all normal, expected behavior. If this is causing you some kind of problem, you haven't explained what it is.

There's no reason to return virtual memory to the operating system because virtual memory isn't a scarce resource. There's no reason to return physical memory to the operating system because the operating system will take it if it has a better use for it anyway.

There's no evidence it's a memory leak. Test results suggest that there is no case where the memory usage increases without bound -- in all cases it levels off eventually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM