Posted over 10 years ago. Visible to the public.

Speed up RSpec by deferring garbage collection

Update: This trick probably isn't very useful anymore in Ruby 2.x. The Ruby GC has improved a lot over the years.

Joe Van Dyk discovered that running the Ruby garbage collector only every X seconds can speed up your tests. I found that deferring garbage collection would speed up my RSpec examples by about 15%, but it probably depends on the nature of your tests. I also tried applying it to Cucumber features, but found no performance improvements.

Since Joe is using Test::Unit in his example, here is how to apply the same trick to RSpec.

First, copy this class to spec/support/deferred_garbage_collection.rb:

class DeferredGarbageCollection DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 10.0).to_f @@last_gc_run = def self.start GC.disable if DEFERRED_GC_THRESHOLD > 0 end def self.reconsider if DEFERRED_GC_THRESHOLD > 0 && - @@last_gc_run >= DEFERRED_GC_THRESHOLD GC.enable GC.start GC.disable @@last_gc_run = end end end

Then, in your spec_helper.rb, configure RSpec like seen below. Note that you might already have code hooked up to before(:all) and after(:all), so merge it together:

Spec::Runner.configure do |config| config.before(:all) do DeferredGarbageCollection.start end config.after(:all) do DeferredGarbageCollection.reconsider end end

You can now enjoy faster specs.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Henning Koch
Last edit:
about 6 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more