Garbage collection outside of request cycle?

Eric Wong normalperson at yhbt.net
Thu May 6 16:57:54 EDT 2010


Luke Melia <luke at lukemelia.com> wrote:
> I've been analyzing our Unicorn-powered Rails app's performance, and
> have found that garbage collection is a big factor in slow requests.
> 
> In the interest of avoiding those performance hits while handling
> requests, would it be possible to have a unicorn worker run garbage
> collection after handling a request and before waiting for the next
> one? Would this be a good idea?

Hi Luke,

I made this for one heavyweight app a while back.

I guess I should throw this into the examples section, but it won't be
the default since it hurts simpler applications that don't generate
much garbage.

==> big_app_gc.rb <==
# This shouldn't hurt overall performance as long as the server cluster
# is at <=50% CPU capacity, and improves the performance of most memory
# intensive requests.  This serves to improve _client-visible_
# performance (possibly at the cost of overall performance).
#
# We'll call GC after each request is been written out to the socket, so
# the client never sees the extra GC hit it. It's ideal to call the GC
# inside the HTTP server (vs middleware or hooks) since the stack is
# smaller at this point, so the GC will both be faster and more
# effective at releasing unused memory.
#
# This monkey patch is _only_ effective for applications that use a lot
# of memory, and will hurt simpler apps/endpoints that can process
# multiple requests before incurring GC.

class Unicorn::HttpServer
  REQ = Unicorn::HttpRequest::REQ
  alias _process_client process_client
  undef_method :process_client
  def process_client(client)
    _process_client(client)
    REQ.clear
    GC.start
  end
end if defined?(Unicorn)
-- 
Eric Wong


More information about the mongrel-unicorn mailing list