[英]Memory leak in PHP with Symfony + Doctrine
I'm using PHP with the Symfony framework (with Doctrine as my ORM) to build a spider that crawls some sites. 我将PHP与Symfony框架(以Doctrine作为我的ORM)一起使用,以构建可对某些站点进行爬网的蜘蛛。
My problem is that the following code generates a memory leak: 我的问题是以下代码会产生内存泄漏:
$q = $this -> createQuery('Product p');
if($store) {
$q
-> andWhere('p.store_id = ?', $store -> getId())
-> limit(1);
}
$q -> andWhere('p.name = ?', $name);
$data = $q -> execute();
$q -> free(true);
$data -> free(true);
return NULL;
This code is placed in a subclass of Doctrine_Table
. 此代码位于
Doctrine_Table
的子类中。 If I comment out the execute part (and of course the $data -> free(true)
) the leak stops. 如果我注释掉执行部分(当然还有
$data -> free(true)
),泄漏将停止。 This has led me to the conclusion that it's the Doctrine_Collection
that's causing the leak. 这使我得出结论,就是导致泄漏的是
Doctrine_Collection
。
I solved my problems with Doctrine memory leaks by freeing and unseting data, did you try it? 我通过释放和取消设置数据来解决了Doctrine内存泄漏的问题,您尝试过吗?
// ...
$data->free(true) ;
unset($data) ;
// ...
What version of PHP are you using? 您正在使用哪个版本的PHP? If it's < 5.3 it probably has to do with the 'recursive references leak memory' bug.
如果它小于5.3,则可能与“递归引用泄漏内存”错误有关。
You can also try to call Doctrine_Manager::connection()->clear()
, which should clean up the connection and remove identity map entries 您也可以尝试调用
Doctrine_Manager::connection()->clear()
,这应该清理连接并删除标识映射条目
I'm just running into the same problem with a CLI command that runs continuously. 我只是遇到连续运行的CLI命令遇到的相同问题。 After spending many hours to troubleshoot this issue it turns out that running my commands in
prod
mode actually solved the issue: 花了许多小时解决此问题后,事实证明,以
prod
模式运行我的命令实际上解决了该问题:
app/console my:custom:command --env=prod
Should you be using addWhere instead of andWhere? 您应该使用addWhere代替andWhere吗? Also I believe a limit should be added at the end of a statement, try:
另外,我认为应该在声明的末尾添加一个限制,请尝试:
$q = $this -> createQuery('Product p') -> where('p.name = ?', $name);
if($store) {
$q
-> addWhere('p.store_id = ?', $store -> getId())
-> limit(1);
}
$data = $q -> execute();
$q -> free(true);
$data -> free(true);
return NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.