class UnicornWrangler::OutOfBandGC

Do not run GC inside of requests, but only after a certain time spent in requests

Alternative: github.com/tmm1/gctools which is more sophisticated and will result in less time spent GCing and less overall memory needed

Public Class Methods

new(logger, stats, max_request_time) click to toggle source
# File lib/unicorn_wrangler.rb, line 183
def initialize(logger, stats, max_request_time)
  @logger = logger
  @stats = stats
  @max_request_time = max_request_time
  @logger.info "Garbage collecting after #{@max_request_time}s of request processing time"
  @gc_ran_at = 0
end

Public Instance Methods

call(_requests, request_time) click to toggle source
# File lib/unicorn_wrangler.rb, line 191
def call(_requests, request_time)
  time_since_last_gc = request_time - @gc_ran_at
  return unless time_since_last_gc >= @max_request_time
  @gc_ran_at = request_time

  time = Benchmark.realtime do
    GC.enable
    GC.start
    GC.disable
  end

  time = (time * 1000).round # s -> ms
  if @stats
    @stats.increment("#{STATS_NAMESPACE}.oobgc.runs")
    @stats.timing("#{STATS_NAMESPACE}.oobgc.time", time)
  end
  @logger.debug "Garbage collecting: took #{time}ms"
  true
end