title: Ruby: How does loading of objects in batches improve memory usage?
body: <p>I'm self taught and not too sure about the terminology or specific improvements there are (if any) from splitting up object loading into separate parts in a loop.</p>
  
  <p>For example, I use rails and recently I've encountered an issue where I was loading too many heavy ActiveRecord objects at once, and found this in the rails API: <a href="http://api.rubyonrails.org/classes/ActiveRecord/Batches.html" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/Batches.html</a></p>
  
  <p>What <code>find_in_batches</code> does is that it separates the query into many subsets so instead of making one large query, you're making 10 small ones and not loading so many objects at once.</p>
  
  <p>Ex:</p>
  
  <pre><code>def batch_process
    Car.find_in_batches do |batch|
      batch.each(&amp;:start_engine!)
    end # at the end of each iteration, is the memory from the current batch deallocated?
  end
  
  def start_all_at_once
    Car.all.each(&amp;:start_engine!)
  end
  </code></pre>
  
  <p>My question is, what exactly is the benefit of doing this? Conceptually I understand that loading less at once allows memory to be freed up on each loop (is this correct??), but what exactly is improved? I believe it's peak memory consumption, but does that translate to RAM / CPU usage improvements (not really sure what the difference between RAM / CPU is to be honest)? Or something to do with Garbage Collection or Ruby heap size?</p>
  
  <p>Just trying to understand the lower level details. Thanks!</p>