Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linjian/3141510 to your computer and use it in GitHub Desktop.
Save linjian/3141510 to your computer and use it in GitHub Desktop.
Deferred Garbage Collection All in One
# http://ariejan.net/2011/09/24/rspec-speed-up-by-tweaking-ruby-garbage-collection
#
# Usage:
# DEFER_GC=10 rspec spec/
# DEFER_GC=10 cucumber features/
#
# put it to spec/support/deferred_garbage_collection_all_in_one.rb
# or feature/support/hooks.rb
class DeferredGarbageCollection
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || -1).to_f
@last_gc_run = Time.now
def self.start
GC.disable if DEFERRED_GC_THRESHOLD > 0
end
def self.reconsider
if DEFERRED_GC_THRESHOLD > 0 && Time.now - @last_gc_run >= DEFERRED_GC_THRESHOLD
GC.enable
GC.start
GC.disable
@last_gc_run = Time.now
end
end
end
if defined?(Cucumber)
# put it to feature/support/hooks.rb
Before do
DeferredGarbageCollection.start
end
After do
DeferredGarbageCollection.reconsider
end
elsif defined?(RSpec)
# put it to spec/spec_helper.rb
RSpec.configure do |config|
config.before(:all) do
DeferredGarbageCollection.start
end
config.after(:all) do
DeferredGarbageCollection.reconsider
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment