我在长时间运行的 perl 脚本中遇到了一些内存泄漏问题,其中 perl 占用的内存量继续增长。因此,我正在尝试使用Devel::Leak来追踪泄漏。我发现每当我调用DBI的prepare
方法时,返回的标量值的数量Devel::Leak
都会增加 1。下面是我整理的测试脚本,可以满足我的描述:
#!/usr/bin/perl
use strict;
use Devel::Leak;
use DBI;
START:
my $handle; # apparently this doesn't need to be anything at all
my $leaveCount = 0;
my $enterCount = Devel::Leak::NoteSV($handle);
print "ENTER: $enterCount SVs\n";
{
# CONFIG VARIABLES
my $platform = "mysql";
my $database = "db";
my $host = "localhost";
my $port = "3306";
my $user = "user";
my $pw = "pass";
#DATA SOURCE NAME
my $dsn = "dbi:mysql:$database:$host:3306";
# PERL DBI CONNECT
my $dbh = DBI->connect($dsn, $user, $pw);
$dbh->prepare("SELECT * FROM table"); # The script seems to gain one SV without this
# line here, but since this is my issue in my
# main script I decided to leave it in
# undef $dbh; I tried undef-ing this, but it made no difference
}
$leaveCount = Devel::Leak::CheckSV($handle);
print "\nLEAVE: $leaveCount SVs\n";
sleep(1);
goto START;
那么我在这里做错了什么,或者这是 DBI 模块中的内存泄漏?另外,我知道每次在循环中添加一个 SV 并不是什么大问题,而且我很可能在其他地方有更大的内存泄漏,这导致 perl 占用了服务器的大量内存。但是,如果可以的话,我仍然想解决这个问题。编码员的好奇心:)
更新:
第一次似乎增加了大约 3,000 个 SV,然后每次增加 1 个。