title: Doctrine ORM Memory issues
body: <p>The Problem:</p>
  
  <p>While running a Daemon service that uses Doctrine from the Factory classes below there is a memory issue. When the Daemon Service starts it runs about 175MB. A day later it's about 250MB, one more day and it's at 400MB. I'm looking as to what is causing the increase in memory and how I could bring it down.</p>
  
  <p>Things I've tried:</p>
  
  <ul>
  <li>$em->clear(); // This kinda helps</li>
  <li>$em->close(); // this causes issues</li>
  <li><p>$em->getConnection()->getConfiguration()->setSQLLogger(null); </p>
  
  <p>--env=prod should take care of setSQLLogger(null), correct?</p></li>
  </ul>
  
  <p>Is there anything I should be doing to help with memory issues using Doctrine 2.x and Symfony 2.1.x?</p>
  
  <p>Created a factory to handle connections</p>
  
  <p>===================== START EMFactory =====================</p>
  
  <pre><code>&lt;?php
  
  namespace NS\Bundle\EMBundle;
  
  use Doctrine\ORM\EntityManager;
  
  class EMFactory
  {
      /**
       * @var
       */
      private $container;
  
      /**
       * @param $container
       */
      public function __construct($container)
      {
          $this-&gt;container = $container;
      }
  
      /**
       * @return EntityManager
       */
      public function getBlahEntityManager()
      {
          return $this-&gt;getContainer()-&gt;get('doctrine.orm.blah_manager_entity_manager');
      }
  
      /**
       * @return EntityManager
       */
      public function getFooEntityManager()
      {
          return $this-&gt;getContainer()-&gt;get('doctrine.orm.foo_manager_entity_manager');
      }
  
      /**
       * @return EntityManager
       */
      public function getBarEntityManager()
      {
          return $this-&gt;getContainer()-&gt;get('doctrine.orm.bar_manager_entity_manager');
      }
  
      /**
       * @return mixed
       */
      public function getContainer()
      {
          return $this-&gt;container;
      }
  
      /**
       * @param $container
       * @return $this
       */
      public function setContainer($container)
      {
          $this-&gt;container = $container;
          return $this;
      }
  
      public function closeEntityManager(EntityManager $em)
      {
          try {
              $em-&gt;clear(); // This kinda helps
              //$em-&gt;close(); // this causes issues
              //$em-&gt;getConnection()-&gt;getConfiguration()-&gt;setSQLLogger(null); // --env=prod should take care of this
          } catch (\Exception $e) {
              // exception here
          }
      }
  }
  </code></pre>
  
  <p>===================== END EMFactory =====================</p>
  
  <p>I use an Abstract Class that constructs the EMFactory</p>
  
  <p>===================== Start Abstract Class =====================</p>
  
  <pre><code>/**
   * @param \Symfony\Component\DependencyInjection\Container $container
   */
  public function __construct(Container $container)
  {
      $this-&gt;container = $container;
      $this-&gt;entityManagerFactory = new EMFactory($container);
  }
  </code></pre>
  
  <p>===================== END Abstract Class =====================</p>
  
  <p>Here is an example of how I'm using the EM, The class extends the Abstract class above</p>
  
  <p>===================== START Working Example #1 =====================</p>
  
  <pre><code>// calling like this looks to be working as expected
  
  $fooEM = $this-&gt;getEntityManagerFactory()-&gt;getFooEntityManager();
  
  $barResults = $fooEM-&gt;getRepository('NS\Bundle\EMBundle\Entity\Bar')-&gt;findOneBy(array('id' =&gt; 1));
  
  if (!is_object($barResults)) {
      throw new \Exception("Bar is a non object.");
  }
  
  // some logic here ...
  
  $this-&gt;getEntityManagerFactory()-&gt;closeEntityManager($fooEM);
  </code></pre>
  
  <p>===================== END Working Example #1 =====================</p>
  
  <p>Here is another example of how I'm using the EM, The class extends the Abstract class above</p>
  
  <p>===================== START Working Example #2 =====================</p>
  
  <pre><code>// calling from functions like this
  
  $fooEM = $this-&gt;getEntityManagerFactory()-&gt;getFooEntityManager();
  
  $dql = 'SELECT b.*
          FROM NS\Bundle\EMBundle\Entity\Bar b            
          WHERE b.id = :id';
  
  $query = $fooEM-&gt;createQuery($dql);
  $query-&gt;setParameter('id', 1);
  
  $barResults = $query-&gt;getResult();
  
  $this-&gt;getEntityManagerFactory()-&gt;closeEntityManager($fooEM);
  
  return $barResults;
  </code></pre>
  
  <p>===================== END Working Example #2 =====================</p>
  
  <p>Here is another example of how I'm using the EM, The class extends the Abstract class above</p>
  
  <p>===================== START Working Example #3 =====================</p>
  
  <pre><code>// calling from functions like this
  
  $fooEM = $this-&gt;getEntityManagerFactory()-&gt;getFooEntityManager();
  
  $barEntity = new Bar();
  $barEntity-&gt;setId(1);
  $barEntity-&gt;setComment('this is foo-ie');
  
  $fooEM-&gt;persist($barEntity);
  $fooEM-&gt;flush();
  
  $this-&gt;getEntityManagerFactory()-&gt;closeEntityManager($fooEM);
  
  unset($barEntity);
  </code></pre>
  
  <p>===================== END Working Example #3 =====================</p>
  
  <p>These are just some basic examples but it's just the queries that get more complex.</p>
  
  <p>Does anything stand out that say, Optimize me? </p>