title: Unicorn Worker Killer is not reducing memory usage
body: <p>I've been struggling with memory issues with my Rails app for the past few days.</p>
  
  <p>I setup a Unicorn server with the sidekiq gem to run background jobs. The jobs were constantly failing in production, so I installed NewRelic to view the memory use, and it showed that the Sidekiq worker was exceeding Heroku's memory limits.</p>
  
  <p>I am now trying to use the unicorn-worker-killer gem to reduce the memory of the worker as the jobs run, but I have not had any success. The logs show this message happening every time I load a page, but it doesn't reduce the memory load.</p>
  
  <pre><code>[2014-12-08T13:52:34.346557 #88418]  WARN -- : #&lt;Unicorn::HttpServer:0x00000101025ee8&gt;: worker    (pid: 88418) exceeds memory limit (123482112 bytes &gt; 105673916 bytes)
  W, [2014-12-08T13:52:34.346793 #88418]  WARN -- : Unicorn::WorkerKiller send SIGQUIT (pid: 88418) alive: 4 sec (trial 1)
  I, [2014-12-08T13:52:34.492893 #88410]  INFO -- : reaped #&lt;Process::Status: pid 88418 exit 0&gt; worker=0
  I, [2014-12-08T13:52:34.501168 #88441]  INFO -- : worker=0 ready
  </code></pre>
  
  <p>Which looks right to me, but the memory taken up by the worker does not go down at all. NewRelic still shows the memory consumption going from 100MB to over 1GB and staying high, without coming down until I restart the server.</p>
  
  <p>Any advice would be tremendously appreciated! I really need my workers to run within memory limits.</p>
  
  <p>Here is my config.ru file: </p>
  
  <pre><code>require 'unicorn/oob_gc'
  use Unicorn::OobGC, 10 # Only GC once every GC_FREQUENCY requests
  
  # --- Start of unicorn worker killer code ---
  
  require 'unicorn/worker_killer'
  
  max_request_min =  500
  max_request_max =  600
  
  # Max requests per worker
  use Unicorn::WorkerKiller::MaxRequests, max_request_min, max_request_max
  
  oom_min = (100) * (1024**2)
  oom_max = (150) * (1024**2)
  
  # Max memory size (RSS) per worker
  use Unicorn::WorkerKiller::Oom, oom_min, oom_max, 1, true
  
  # --- End of unicorn worker killer code ---
  
  
  
  # This file is used by Rack-based servers to start the application.
  
  require ::File.expand_path('../config/environment',  __FILE__)
  run Rails.application
  </code></pre>
  
  <p>And here is the Unicorn.rb file:</p>
  
  <pre><code>worker_processes Integer(1)
  timeout 30
  preload_app true
  stderr_path "log/unicorn.stderr.log"
  stdout_path "log/unicorn.stdout.log"
  
  before_fork do |server, worker|
    Signal.trap 'TERM' do
      puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
      Process.kill 'QUIT', Process.pid
    end
  
    if defined?(ActiveRecord::Base)
      ActiveRecord::Base.connection.disconnect!
    end
  
    old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
      begin
        sig = (worker.nr + 1) &gt;= server.worker_processes ? :QUIT : :TTOU
        Process.kill(sig, File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
      end
    end
  end
  
  after_fork do |server, worker|
    Signal.trap 'TERM' do
      puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
    end
  
    if defined?(ActiveRecord::Base)
      config = ActiveRecord::Base.configurations[Rails.env] ||
              Rails.application.config.database_configuration[Rails.env]
      config['reaping_frequency'] = 10 #seconds
      config['pool']            =   ENV['DB_POOL']
      ActiveRecord::Base.establish_connection(config)
    end
  end
  </code></pre>