title: Doctrine Batch Processing Iterate High Memory Usage
body: <p>I've been looking into using iterators for batch processing in Doctrine (<a href="http://docs.doctrine-project.org/en/2.0.x/reference/batch-processing.html" rel="noreferrer">http://docs.doctrine-project.org/en/2.0.x/reference/batch-processing.html</a>). I've got a database with 20,000 images which I would like to iterate over.</p>
  
  <p>I understand that using an iterator is supposed to prevent Doctrine from loading every row in memory. However the memory usage between the two examples is almost exactly the same. I am calculating the memory usage before and after using <code>(memory_get_usage() / 1024)</code>.</p>
  
  <pre><code>$query = $this-&gt;em-&gt;createQuery('SELECT i FROM Acme\Entities\Image i');
  $iterable = $query-&gt;iterate();
  
  while (($image = $iterable-&gt;next()) !== false) {
      // Do something here!
  }
  </code></pre>
  
  <p>Memory usage for the iterator.</p>
  
  <pre><code>Memory usage before: 2823.36328125 KB
  Memory usage after: 50965.3125 KB
  </code></pre>
  
  <p>This second example loads the entire result set into memory using the <code>findAll</code> method.</p>
  
  <pre><code>$images = $this-&gt;em-&gt;getRepository('Acme\Entities\Image')-&gt;findAll();
  </code></pre>
  
  <p>Memory usage for <code>findAll</code>.</p>
  
  <pre><code>Memory usage before: 2822.828125 KB
  Memory usage after: 51329.03125 KB
  </code></pre>