Skip to content
Advertisement

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:

$entityManager = $this->getContainer()->get('doctrine')->getManager();
$myRepository = $entityManager->getRepository(MyEntity::class);
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.

Advertisement

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement