Jonathan Jonathan - 20 days ago 11
PHP Question

Memory leak when executing Doctrine query in loop

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.

Answer

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.