【发布时间】:2014-12-24 08:39:59
【问题描述】:
我无法在脚本中找到内存泄漏的原因。我有一个简单的存储库方法,它将我的实体中的“计数”列增加 X 数量:
public function incrementCount($id, $amount)
{
$query = $this
->createQueryBuilder('e')
->update('MyEntity', 'e')
->set('e.count', 'e.count + :amount')
->where('e.id = :id')
->setParameter('id', $id)
->setParameter('amount', $amount)
->getQuery();
$query->execute();
}
问题是,如果我在循环中调用它,每次迭代时内存使用量都会激增:
$entityManager = $this->getContainer()->get('doctrine')->getManager();
$myRepository = $entityManager->getRepository(MyEntity::class);
while (true) {
$myRepository->incrementCount("123", 5);
$doctrineManager->clear();
gc_collect_cycles();
}
我在这里缺少什么?根据 Doctrine 的 advice on batch processing,我已经尝试过 ->clear()。我什至尝试过gc_collect_cycles(),但问题仍然存在。
我在 PHP 5.5 上运行 Doctrine 2.4.6。
【问题讨论】:
-
在你的函数中尝试作为最后一行
$query->clear(); -
你将无限循环使用 while(true);是否有休息、返回或退出某处?
-
@Mihai 这只是给出了一个
Call to undefined method Doctrine\ORM\Query::clear()错误。 -
@HalayemAnis 是的,我只是将其从示例中删除以保持我的问题简单。
-
我认为您可以通过配置与数据库的持久连接来解决您的问题
标签: php symfony memory-leaks doctrine-orm doctrine