Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1682

EntityManager::clear() not working as expected.

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Incomplete
    • Affects Version/s: 2.1.6
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      Ubuntu 11.10
      Symfony 2

      Description

      I've been reading Doctrine2 Batch Processing documentation.
      I've a simplified the code and made a sample where I'm using a Repository inside the loop:

       
      for ($i=1; $i<=10000; ++$i) {
           $user = $this->_em->getRepository('some user class')->find($i);
           $this->_em->clear();
      
           //Clear variables to ensure garbage collections     
           unset($user);
           $user = null;
      
      }
      

      I expect that this script will consume some constant memory in all iterations, but what happens is that every iteration raises memory consumption (more iterations, more memory).... i think that the clear method has some sort of memory leak.

      In my production environment (with complex script), i reach a memory limit exception even with 600MB limit... but if I clear the EntityManager on every iteration, shouldn't memory be freed?

      Sorry for my bad english.

        Activity

        Hide
        mathias dusautoy added a comment - - edited

        without symfony:

        <?php
        
        use Doctrine\ORM\Tools\Setup;
        use Doctrine\ORM\EntityManager;
        
        $loader = require_once __DIR__.'/../app/autoload.php';
        $loader->add('Acme\\CoreBundle', __DIR__.'/../src/Acme/CoreBundle/');
        
        $isDevMode = true;
        $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/../src/Acme/CoreBundle/Entity"), $isDevMode, null, null, false);
        
        $conn = array(
        	'driver' => 'pdo_mysql',
        	'host' => 'localhost',
        	'dbname' => 'dbname',
        	'user' => 'root',
        	'password' => ''
        );
        
        $em = EntityManager::create($conn, $config);
        
        $d = new \DateTime();
        
        while(true) {
        	echo memory_get_usage() . PHP_EOL;
        	$qb = $em->createQueryBuilder()
        		->select('c')
        		->from('Acme\\CoreBundle\\Entity\\Consultation', 'c')
        		->where('c.date > :date')->setParameter(':date', $d)
        		->orderBy('c.date', 'ASC');
        
        	$q = $qb->getQuery();
        
        	$results = $q->getResult();
        
        	foreach($results as $c) {
        		echo $c->getDate()->format('H:i:s') . PHP_EOL;
        	}
        	
        	$q->free();
        
        	$em->clear();
        
        	gc_collect_cycles();
        	
        	echo memory_get_usage() . PHP_EOL . PHP_EOL;
        }
        

        output:

        7978568
        7978568
        
        7978568
        7978568
        
        7978568
        11:51:27
        11474520
        
        11474520
        11473368
        
        11473368
        11473368
        
        11473368
        11473368
        
        11473368
        11473368
        

        Am I missing something?

        Show
        mathias dusautoy added a comment - - edited without symfony: <?php use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; $loader = require_once __DIR__.'/../app/autoload.php'; $loader->add('Acme\\CoreBundle', __DIR__.'/../src/Acme/CoreBundle/'); $isDevMode = true ; $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__. "/../src/Acme/CoreBundle/Entity" ), $isDevMode, null , null , false ); $conn = array( 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => 'dbname', 'user' => 'root', 'password' => '' ); $em = EntityManager::create($conn, $config); $d = new \DateTime(); while ( true ) { echo memory_get_usage() . PHP_EOL; $qb = $em->createQueryBuilder() ->select('c') ->from('Acme\\CoreBundle\\Entity\\Consultation', 'c') ->where('c.date > :date')->setParameter(':date', $d) ->orderBy('c.date', 'ASC'); $q = $qb->getQuery(); $results = $q->getResult(); foreach($results as $c) { echo $c->getDate()->format('H:i:s') . PHP_EOL; } $q->free(); $em->clear(); gc_collect_cycles(); echo memory_get_usage() . PHP_EOL . PHP_EOL; } output: 7978568 7978568 7978568 7978568 7978568 11:51:27 11474520 11474520 11473368 11473368 11473368 11473368 11473368 11473368 11473368 Am I missing something?
        Hide
        Marco Pivetta added a comment -

        Memory usage here seems quite constant (the change from 7978568 to 11474520 may well be because of metadata and hydrators). The output doesn't seem to be conforming your snippet though.

        Show
        Marco Pivetta added a comment - Memory usage here seems quite constant (the change from 7978568 to 11474520 may well be because of metadata and hydrators). The output doesn't seem to be conforming your snippet though.
        Hide
        mathias dusautoy added a comment - - edited

        yes sorry the above output is for:

        foreach($results as $c) {
            echo $c->getDate()->format('H:i:s') . PHP_EOL;
            $d = $c->getDate();
        }
        

        with

        foreach($results as $c) {
            echo $c->getDate()->format('H:i:s') . PHP_EOL;
        }
        

        the output is:

        3489864
        12:22:27
        13502680
        
        13502680
        12:22:27
        13515496
        
        13515496
        12:22:27
        13528328
        
        13528328
        12:22:27
        13541144
        
        13541144
        12:22:27
        13553976
        
        ....
        
        
        74513560
        12:22:27
        74526520
        
        74526520
        12:22:27
        74539520
        
        74539520
        12:22:27
        74552560
        

        and goes on

        Show
        mathias dusautoy added a comment - - edited yes sorry the above output is for: foreach($results as $c) { echo $c->getDate()->format('H:i:s') . PHP_EOL; $d = $c->getDate(); } with foreach($results as $c) { echo $c->getDate()->format('H:i:s') . PHP_EOL; } the output is: 3489864 12:22:27 13502680 13502680 12:22:27 13515496 13515496 12:22:27 13528328 13528328 12:22:27 13541144 13541144 12:22:27 13553976 .... 74513560 12:22:27 74526520 74526520 12:22:27 74539520 74539520 12:22:27 74552560 and goes on
        Hide
        Christophe Coevoet added a comment -

        Do you have bidirectional relations in your user entity ? If yes, you will still have some references to the object after clearing the EntityManager (in the related object, itself reference by the user)

        Show
        Christophe Coevoet added a comment - Do you have bidirectional relations in your user entity ? If yes, you will still have some references to the object after clearing the EntityManager (in the related object, itself reference by the user)
        Hide
        Bruno Lemos added a comment - - edited

        Fixed it by doing the following:

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

        Show
        Bruno Lemos added a comment - - edited Fixed it by doing the following: $this ->_em ->getConnection() ->getConfiguration() ->setSQLLogger(null);

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            German Caseres
          • Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: