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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With