title: Unicorn memory leak on Heroku when dyno is idle
body: <p>Short version</p>
  
  <p>Memory leak and R14 errors on Heroku with Rails + Unicorn when there are no requests at all. No problem locally, or with Webrick, or when the server is under a load.</p>
  
  <p>Long version</p>
  
  <p>I have a Rails 4 application running on Heroku. When there is no load on the server, the memory consumption reported by Heroku goes up around 0.5 MB per minute. After a few hours, R14 errors starting to appear in the logs, and a while later the process does not respond and Heroku kills it.
  I can see the increasing memory usage in New Relic as well.</p>
  
  <p><code>02:03:00 heroku[web.1]: source=web.1 sample#load_avg_1m=0.00 sample#load_avg_5m=0.00 sample#load_avg_15m=0.00
  02:03:00 heroku[web.1]: source=web.1 sample#memory_total=533.19MB sample#memory_rss=511.88MB sample#memory_cache=0.00MB sample#memory_swap=21.30MB sample#memory_pgpgin=1034783pages sample#memory_pgpgout=903740pages
  02:03:00 heroku[web.1]: Process running mem=533M(104.1%)</code></p>
  
  <p>Environment:
  Ruby 2.1.4 + Rails 4.1.7 + Unicorn + Heroku with 1 web dyno</p>
  
  <p>What I tried so far</p>
  
  <ol>
  <li><p>Decreased the number of Unicorn workers from the default 3 to 2. Did not help, just made the memory leak a little bit slower.</p></li>
  <li><p>Disabled background workers (removed from Procfile). No effect. Did not change anything.</p></li>
  <li><p>Installed Unicorn Worker Killer. It did not help because there are no requests going on, so the worker killer never kicks in (is there a time based worker killer?).</p></li>
  <li><p>Used ApacheBench to stress test the server. The memory usage was constant, around 190MB/process according to New Relic. There was no increase. It seems like the memory leak only happens when there is no server load.</p></li>
  <li><p>Removed Unicorn and used the default Webrick. The memory usage became constant, around 208MB/instance according to New Relic!</p></li>
  <li><p>Tried to reproduce this issue on local machine by starting the server in production with <code>RAILS_ENV=production  foreman start</code>. There was no memory leak in this case.</p></li>
  </ol>
  
  <p>Based on point 5, I think the memory leak is related to Unicorn but somehow only happens on Heroku.
  How to track down where is the problem? </p>
  
  <p>Thanks! Any help is appreciated.</p>