Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running out of band garbage collection with Unicorn + Rack

I am attempting to run garbage collection out of band (once a request has finished serving its response) in my Ruby on Rails application. I added the following to my config.ru:

# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment',  __FILE__)
begin
  require 'unicorn/oob_gc'
rescue LoadError, NameError
end
# Out-of-band GC, runs GC after every 10th request and after the response
# has been delivered.
begin
  use Unicorn::OobGC, interval=10
rescue NameError
end

run MyApp::Application

GC.start

I'm looking at my NewRelic portal however, and most web transactions do indicate that at least 110-150ms is spent on average doing garbage collection. Is Unicorn::OoobGC supposed to do it out of the scope of the actual request? If it is, why is this showing up in the web transaction? How do I get time spent on garbage collection to happen outside the context of a web request so that perceived client response times are faster? CPU time spent will still be the same since it needs to happen in the background, however, better in the background than holding up a request pipeline.

like image 771
randombits Avatar asked Nov 19 '12 23:11

randombits


1 Answers

If a single request allocates enough objects to trigger a GC you will still see gc time reported for the request despite moving a final GC OOB with the unicorn middleware.

With ruby 1.9.3 and REE you can twiddle various GC knobs to help control how often gc gets triggered. See Tuning the GC in Ruby 1.9.3 for examples on setting RUBY_HEAP_MIN_SLOTS, RUBY_GC_MALLOC_LIMIT, and RUBY_FREE_MIN for better behavior in long running service apps.

like image 181
dbenhur Avatar answered Oct 10 '22 19:10

dbenhur