Understand Why GC in Ruby 2.1 and 2.2 Is So Much Faster

In this book we repeatedly observed that new Ruby versions consistently perform better because the GC is faster. But why is it faster?

The first reason it’s faster is that less GC is needed. We have just seen that with our memory allocation example. The second reason is that each individual GC run can take less time.

Ruby implements GC using a simple two-phase mark and sweep (M&S) algorithm. In the mark phase it finds all living objects on the Ruby heap and marks them as live. In the sweep phase it collects unmarked objects.

Naturally, GC can’t allow you to allocate new objects while it marks. So your program pauses for the duration of GC.

Ruby 2.1 with RGenGC reduces the number ...

Get Ruby Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.