Jonathan Jonathan - 1 year ago 80
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
->update('MyEntity', 'e')
->set('e.count', 'e.count + :amount')
->where(' = :id')
->setParameter('id', $id)
->setParameter('amount', $amount)


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);

What am I missing here? I've tried
, as per Doctrine's advice on batch processing. I even tried
, but still the issue remains.

I'm running Doctrine 2.4.6 on PHP 5.5.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download