examples/big_app_gc: update this example
[unicorn.git] / examples / big_app_gc.rb
blob0d9aa541b89f00e32a14f584e7b92f2292b79018
1 # Run GC after every request, before attempting to accept more connections.
3 # You could customize this patch to read REQ["PATH_INFO"] and only
4 # call GC.start after expensive requests.
6 # We could have this wrap the response body.close as middleware, but the
7 # scannable stack is would still be bigger than it would be here.
9 # This shouldn't hurt overall performance as long as the server cluster
10 # is at <=50% CPU capacity, and improves the performance of most memory
11 # intensive requests.  This serves to improve _client-visible_
12 # performance (possibly at the cost of overall performance).
14 # We'll call GC after each request is been written out to the socket, so
15 # the client never sees the extra GC hit it. It's ideal to call the GC
16 # inside the HTTP server (vs middleware or hooks) since the stack is
17 # smaller at this point, so the GC will both be faster and more
18 # effective at releasing unused memory.
20 # This monkey patch is _only_ effective for applications that use a lot
21 # of memory, and will hurt simpler apps/endpoints that can process
22 # multiple requests before incurring GC.
24 module Unicorn::BigAppGC
25   def process_client(client)
26     super
27     @request.clear
28     GC.start
29   end
30 end
31 ObjectSpace.each_object(Unicorn::HttpServer) { |s| s.extend(Unicorn::BigAppGC) }