Skip to content

Instantly share code, notes, and snippets.

@janx
Created May 2, 2013 11:02
Show Gist options
  • Save janx/5501506 to your computer and use it in GitHub Desktop.
Save janx/5501506 to your computer and use it in GitHub Desktop.
GC trick, reduce gc frequency by time factor, create hiccups in response time
class GCMiddleware
def initialize(app, options={})
@app = app
@options = options
end
def call(env)
begin_gc_deferment
result = @app.call(env)
reconsider_gc_deferment(env)
result
end
DEFERRED_GC_THRESHOLD = 5.0
@@last_gc_run = Time.now
private
def begin_gc_deferment
GC.disable if DEFERRED_GC_THRESHOLD > 0
end
def reconsider_gc_deferment(env)
if DEFERRED_GC_THRESHOLD > 0 && Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD
request = Rack::Request.new(env)
GC.enable
GC::Profiler.enable
GC.start
puts request.path
puts GC::Profiler.result
GC::Profiler.disable
GC::Profiler.clear
GC.disable
@@last_gc_run = Time.now
end
end
end
use GCMiddleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment