我在运行我的脚本之一时不断收到此错误;
PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 71 bytes) in ... lib/symfony-1.4.11/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/Statement.php on line 246, ...
以下是脚本的精简版 触发错误;
public function executeImportFile(sfWebRequest $request)
{
ini_set('memory_limit', '1024M');
set_time_limit ( 0 );
//more codes here...
$files = scandir($workspace.'/'.$directory);
foreach ($files as $file) {
$path = $workspace.'/'.$directory.'/'.$file;
if ($file != "." && $file != "..") {
$this->importfile($path);
}
}
}
protected function importfile($path){
$connection =
sfContext::getInstance()->getDatabaseManager()->getDatabase('doctrine')->getDoctrineConnection();
$connection->beginTransaction();
try {
//more codes here...
while ($data = $reader->read()) //reads each line of a csv file
{
// send the line to another private function to be processed
// and then write to database
$this->writewave($data);
}
$connection->commit();
} catch (Exception $e) {
$connection->rollback();
}
}
脚本所做的基本上是读取一个文件中的所有 csv 文件 文件夹(每个文件夹包含数万行),对其进行处理, 并使用 Doctrine 的事务将其写入数据库。
虽然我觉得不需要设置内存限制和时间限制 在这两个函数中,脚本退出,因为 Doctrine 用完了所有的 分配了 1GB 的内存。
一般处理完10个文件就会停止,再分配更多 内存将允许它处理更多的文件,并且仍然会 崩溃。
有什么我在这里遗漏的导致内存不 处理每个文件后释放?
Mohd Shakir Zakaria http://www.mohdshakir.net
最佳答案
尽可能释放任何对象,包括查询/连接对象,尤其是当它们在循环内时。
关于php - 使用过多内存问题的 Doctrine 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6079918/