0
我傳遞2個具有1000萬行(1行是最大200字符)的地圖到下面的函數。此功能導致內存不足錯誤。我是perl的新手,有人能幫我弄清楚我在哪裏導致內存泄漏。我的perl程序在哪裏泄漏內存?
sub createDataFile{
my($rowIdToBatchIdMap_ref, $batchIdToNFSFileNameMap_ref, $reportDataCsvFileName, $reportDataCsvFileNameToCreate) = @_;
open FILETOREAD, $reportDataCsvFileName or die "unable to open a file $reportDataCsvFileName for reading reports data. $!";
open FILETOWRITE, ">".$reportDataCsvFileNameToCreate or die "unable to open a file $reportDataCsvFileNameToCreate for writing reports data. $!";
my $countOfReportHavingNullBatchId = 0;
my $countOfMissingBatchIdInBatchTable = 0;
my $countOfMissingTransRefNoInReportTable = 0;
#reading file line by line
while(<FILETOREAD>){
my @fields = split(',', $_);
my $reportRowId = $fields[0];
if(! exists($rowIdToBatchIdMap_ref->{$reportRowId})){
$logger->log(Debug => "key do not exist in rowIdToBatchIdMap for row id $reportRowId");
my $replacedLine = $fields[0]. ",," . $fields[1];
print FILETOWRITE $replacedLine;
$countOfReportHavingNullBatchId++;
next;
} else {
my $batchId = $rowIdToBatchIdMap_ref->{$reportRowId};
$batchId = trim($batchId);
if(! exists($batchIdToNFSFileNameMap_ref->{$batchId})){
$logger->log(Debug => "key do not exist in batchIdToNFSFileNameMap_ref for batch id $batchId");
my $replacedLine = $fields[0]. ",," . $fields[1];
print FILETOWRITE $replacedLine;
$countOfMissingBatchIdInBatchTable++;
} else {
my $xmlFileName = $batchIdToNFSFileNameMap_ref->{$batchId};
my $replacedLine = $fields[0]. "," . $xmlFileName . "," . $fields[1];
print FILETOWRITE $replacedLine;
}
}
} #end of while
$logger->log(Info => "No of reports having null batch id in REPORT_DATA table is $countOfReportHavingNullBatchId");
$logger->log(Info => "No of reports whose batch id is not present in BATCH table is $countOfMissingBatchIdInBatchTable");
close(FILETOREAD);
close(FILETOWRITE);
}
什麼是內存消耗?我們在談論什麼數量? – user973254
我的腳本在使用3.5 GB後因內存不足而死亡,因爲兩個輸入映射使用的內存都在600 MB左右,所以不應使用它。 – jayesh
就我所見,這個子程序只使用常量內存。雖然可以改進,但很可能不是問題的根源。你確定*在執行這段代碼時拋出了內存不足的異常嗎?究竟在哪裏發生? (Hunches:可能在打開文件時,更可能是'$ logger'正在做的事情) – amon