perl 内存问题

Landlord 2009-03-05 11:14:26
Windows平台。下面的代码,我开启一个线程来接收文件,但是运行几次,发现这个脚本耗用内存简直是疯长,但是线程执行完后没见内存被回收。这是个什么原因来着?各位帮参谋一下:
#!perl -w

use strict;
use IO::Socket;

my $dir = shift @ARGV;
if (!defined ($dir)) {
print "请指定一个目录存放接收的文件!\n";
exit (-1);
}

if (!-e $dir) {
mkdir $dir || die "无法创建目录,@!";
}

printf "已经启动了服务器,正等接收文件,并放在目录 \'%s\' 下。\n", $dir;

my $listen_socket = IO::Socket::INET->new (LocalPort => 9999,
Listen => 20,
Proto => 'tcp',
Reuse => 1);

if (!defined ($listen_socket)) {
die "未能启动接收服务器,$@";
}

use File::Path;
use threads;

while (1) {
my $connection = $listen_socket->accept ();
if (!defined ($connection)) {
next;
}
# 有新连接开启一个线程来处理
threads->create (\&receive_result_file, $connection);
}

sub receive_result_file
{
my $connection = shift (@_);
my $client = $connection->peerhost ();
my $file;
my $buf_len = 2048;
#
# 这里取得文件名。
#
sysread ($connection, $file, $buf_len) or die "无法读取数据。\n";
syswrite ($connection, "OK", 2);

my $buffer;
my $full_file = $dir;
if ($^O !~ /MSWin32/i) {
$full_file .= "/";
}
else {
$full_file .= "\\";
}

$full_file .= $file;

#
# 取得路径
#
my $full_dir;
if ($full_file =~ /(.+?)([\\\/]?[^\\\/]+)$/) {
$full_dir = $1;
}

if ((length ($full_dir) > 0) && ($full_dir ne '.' || $full_dir ne '..') && !-e $full_dir) {
mkpath ($full_dir) || next;
}

if (!open (THIS_FILE, "+>$full_file")) {
printf "无法打开文件 %s。\n", $full_file;
next;
}

binmode (THIS_FILE);

printf "接收来自机器 %s 的文件 %s ... \n", $client, $file;

#
# 下面读取文件的数据,当客户端完成传送一个文件时,会关掉连接,以此来判断一个文件是否传送完成。
#
my $data_len;
while ($data_len = sysread ($connection, $buffer, $buf_len)) {
syswrite ($connection, "OK", 2);
if (!defined (syswrite (THIS_FILE, $buffer, $data_len))) {
close (THIS_FILE);
next;
}
undef $buffer;
}

close (THIS_FILE);
$connection->shutdown (1);
return 0;
}
...全文
274 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
adelechen 2009-03-24
  • 打赏
  • 举报
回复
弱弱的问一句,你为什么不用threads->exit
iambic 2009-03-24
  • 打赏
  • 举报
回复
涨了多少?
fibbery 2009-03-09
  • 打赏
  • 举报
回复
还没有用过perl的线程,但是,一般来说,线程创建后,要么连接要么分离,否则资源只能等到程序退出时才能够释放。
wxlfh 2009-03-08
  • 打赏
  • 举报
回复
Perl5.8的线程应该已经比较稳定了吧?Perl5.6的时候的确不太稳定。
Landlord 2009-03-06
  • 打赏
  • 举报
回复
我故意等待了很长时间,再查看其占用的内存,一直保持在最后的使用量一直没有变,而我用的是Perl 5.10.0.在邮件列表上,他们也说是Perl某版本的线程会存在内存泄露。但是好像5.10.0是当前那个有问题题版本之后的版本了吧?
kindTerry 2009-03-05
  • 打赏
  • 举报
回复
另外請嘗試使用高版本的perl來運行你的這個程序
kindTerry 2009-03-05
  • 打赏
  • 举报
回复
lz,線程結束后多久內存不見下降?
Aylazhang 2009-03-05
  • 打赏
  • 举报
回复
我只知道perl5.8的线程不稳定,能不用就别用。

37,716

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧