简体   繁体   English

Perl线程中的内存泄漏(线程不会释放内存)

[英]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: 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: 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) 第一个代码仅运行一个无限循环,并且大约每2秒将其释放回操作系统(根据ps)

The second code is releasing memory too but only when he's running inside the "for" loop. 第二个代码也释放内存,但仅当他在“ for”循环中运行时才释放内存。 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. 一旦退出for循环并进入无限循环,所有尚未释放到for循环中的内存就永远不会释放回操作系统。

Is anybody experiencing the same issue ? 是否有人遇到过同样的问题?

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

OS: Debian 6.0.5 操作系统:Debian 6.0.5

Thanks a lot 非常感谢


Edit 1: I used 800 threads and all verified that they exit by printing the $ee var. 编辑1:我使用了800个线程,并且所有线程都通过打印$ ee var来验证它们是否退出。

But once entering the while(1) loop here's the ps aux | 但是一旦进入while(1)循环,这就是ps aux | grep perl output: grep perl输出:

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. 因此所有线程都退出了,但是内存使用率仍然是服务器总内存的2.5%。 So unless I kill the program the memory is still in use. 因此,除非我终止该程序,否则内存仍在使用中。


Edit 2: 编辑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. 测试结果表明,在任何情况下都不会出现内存使用无限增加的情况-在所有情况下,内存使用情况最终都会趋于平稳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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