在循环中执行Doctrine查询时出现内存泄漏 [英] Memory leak when executing Doctrine query in loop
问题描述
public function incrementCount ,$ 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();
}
问题是,如果我在循环中调用这个内存使用气球迭代:
$ doctrineManager = $ this-> getContainer() - > get('doctrine') - > getManager ();
$ myRepository = $ doctrineManager-> getRepository('MyEntity');
while(true){
$ myRepository-> incrementCount(123,5);
$ doctrineManager-> clear();
gc_collect_cycles();
}
我在这里缺少什么?我试过 - > clear()
,根据Doctrine的关于批处理的建议。我甚至试过 gc_collect_cycles()
,但仍然存在问题。
我正在运行Doctrine 2.4.6 on PHP 5.5。
我通过添加 - no-debug
我的命令事实证明,在调试模式下,分析器正在存储有关内存中每个单个查询的信息。
I'm having trouble in locating the cause for a memory leak in my script. I have a simple repository method which increments a 'count' column in my entity by X amount:
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();
}
Problem is, if I call this in a loop the memory usage balloons on every iteration:
$doctrineManager = $this->getContainer()->get('doctrine')->getManager();
$myRepository = $doctrineManager->getRepository('MyEntity');
while (true) {
$myRepository->incrementCount("123", 5);
$doctrineManager->clear();
gc_collect_cycles();
}
What am I missing here? I've tried ->clear()
, as per Doctrine's advice on batch processing. I even tried gc_collect_cycles()
, but still the issue remains.
I'm running Doctrine 2.4.6 on PHP 5.5.
I resolved this by adding --no-debug
to my command. It turns out that in debug mode, the profiler was storing information about every single query in memory.
这篇关于在循环中执行Doctrine查询时出现内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!