Memory leaks Symfony2 Doctrine2 / exceed memory limit Memory leaks Symfony2 Doctrine2 / exceed memory limit symfony symfony

Memory leaks Symfony2 Doctrine2 / exceed memory limit


As stated by the Doctrine Configuration Reference by default logging of the SQL connection is set to the value of kernel.debug, so if you have instantiated AppKernel with debug set to true the SQL commands get stored in memory for each iteration.

You should either instantiate AppKernel to false, set logging to false in you config YML, or either set the SQLLogger manually to null before using the EntityManager

$em->getConnection()->getConfiguration()->setSQLLogger(null);


Try running your command with --no-debug. In debug mode the profiler retains informations about every single query in memory.


1. Turn off logging and profiling in app/config/config.yml

doctrine:    dbal:        driver: ...        ...        logging: false        profiling: false

or in code

$this->entityManager->getConnection()->getConfiguration()->setSQLLogger(null);

2. Force garbage collector. If you actively use CPU then garbage collector waits and you can find yourself with no memory soon.

At first enable manual garbage collection managing. Run gc_enable() anywhere in the code. Then run gc_collect_cycles() to force garbage collector.

Example

public function execute(InputInterface $input, OutputInterface $output){    gc_enable();    // I'm initing $this->entityManager in __construct using DependencyInjection    $customers = $this->entityManager->getRepository(Customer::class)->findAll();    $counter = 0;    foreach ($customers as $customer) {        // process customer - some logic here, $this->em->persist and so on        if (++$counter % 100 == 0) {            $this->entityManager->flush(); // save unsaved changes            $this->entityManager->clear(); // clear doctrine managed entities            gc_collect_cycles(); // PHP garbage collect            // Note that $this->entityManager->clear() detaches all managed entities,            // may be you need some; reinit them here        }    }    // don't forget to flush in the end    $this->entityManager->flush();    $this->entityManager->clear();    gc_collect_cycles();}

If your table is very large, don't use findAll. Use iterator - http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/batch-processing.html#iterating-results