title: Iterating over doctrine collections takes too much memory
body: <p>I've a symfony2 command that skims over my big database and exports the data into an XML file.</p>
  
  <p>This operation takes too much memory and I can see my php process when running starting to take 50 MB, then 100Mb .. and after 5 minutes it's 700MB and before it's done it takes ~800MB which is obviously huge.</p>
  
  <p>How do I optimize the amount of memory used by Doctrine?</p>
  
  <p>Below's how my code looks like:</p>
  
  <pre><code>  // Gets 4000 entities
    $entities1 = $this-&gt;doctrine-&gt;getRepository('MyBundle:Entity1')-&gt;findAll();
  
  
      foreach ($entities1 as $entity1)
      {
           // 200 entities under every entity1
           foreach ($entity1-&gt;getCollection1() as $c)
           {
                 // write into an xml
           }
      }
  </code></pre>
  
  <p>Is there a way to optimize this/to do it better?</p>