16

I recently deployed an upgrade of Ruby from 2.0 to 2.1.5 to my Heroku web application, and I am consistently hitting memory quota errors now, whereas with 2.0 and 1.9 this never happened. There is a limit of 512MB for a normal Heroku Dyno, and I am running 2 processes with Unicorn, along with one thread with Sidekiq across two dynos.

After reading Phusion Passenger memory consumption increase from 1.9.3 (system) to 2.1.2 (RVM) on Ubuntu, I tried setting the environment variable RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR to 0.9 to disable generational garbage collector, and it did improve memory consumption somewhat, but still left me nowhere near what 2.0 or 1.9 consumed. As memory right now is my primary concern, I would like to see if I can fix this problem with ruby 2.1.x instead of reverting to 2.0.

Here are some graphs demonstrating the problem:

At just before 2pm, I downgraded to Ruby 2.0.0-p598 to Ruby 2.1.5 and the memory issues were resolved and remained under the limit.

moving from 2.1.5 to 2.0.0 Heroku moving from 2.1.5 to 2.0.0 New Relic

Then later, I tried upgrading to Ruby 2.1.5 but setting the environment variable RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR to 0.9. As you can see, the memory usage is slightly better than the original 2.1.5 graph, but still breaches the memory quota.

moving from 2.0.0 to 2.1.5 with <code>RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR</code> to 0.9 Heroku moving from 2.0.0 to 2.1.5 with <code>RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR</code> to 0.9 New Relic

Community
  • 1
  • 1
Matthew O'Riordan
  • 7,981
  • 4
  • 45
  • 59
  • very interesting. I would have thought [this](https://www.omniref.com/blog/blog/2014/11/18/ko1-at-rubyconf-2014-massive-garbage-collection-speedup-in-ruby-2-dot-2/) was related but curious to see what the answer is here. – Anthony Nov 24 '14 at 12:46
  • Been following the 2.2 release too, but I am concerned that their focus is largely on performance improvements as opposed to memory consumption improvements, so I suspect it won't help much. – Matthew O'Riordan Nov 24 '14 at 15:57
  • Some early testers reported lower memory footprint due to updated GC in 2.0.0-preview1. You might want to check it out. – Lenart Dec 01 '14 at 13:09
  • @Lenart, Did you mean 2.2.0-preview1? – Matthew O'Riordan Dec 01 '14 at 17:10
  • @MatthewO'Riordan that's correct. 2.2 of course. My bad! – Lenart Dec 02 '14 at 11:20
  • 2 things; 1: Where are you getting these awesome graphs from? 2: Have you considered moving to [thin](http://code.macournoyer.com/thin/). I believe that might reduce your memory footprint, but I am not sure. I also see that in some tests Ruby Enterprise Edition appears to have much lower memory footprint (about half in some tests I saw :O) – Automatico May 31 '15 at 02:06

2 Answers2

13

So the answer is clearly upgrade to Ruby version 2.2.

I am impressed to see that memory usage has actually gone down following the upgrade to 2.2, whereas upgrading to 2.1 was a huge increase in memory consumption. See the graph below.

Memory usage with 2.2

On the far left memory consumption on 2.0 sits at around 386mb when under load, whereas with 2.2 it now sits at around 365mb.

Matthew O'Riordan
  • 7,981
  • 4
  • 45
  • 59
3

This is a known issue with Ruby (versions 2.1.x) and its garbage collection. After reading couple of forums/blog posts there seems to be no real solution but to do one of the following:

  • downgrade to ruby 2.0 and wait for ruby 2.2 to be released
  • use unicorn worker killer to restart unicorn workers once they reach certain amount of memory, preventing Heroku R14 errors
  • some suggest tweaking your GC variables (see here and here)

Here's a link to relevant discussion on ruby-lang.

Some people also noticed that one source of the problems could be NewRelic's gem so you might want to update/remove it and see if it helps.

Credits for some of the links and info to guys from upcase.com forum

Lenart
  • 3,101
  • 1
  • 26
  • 28
  • Regarding NewRelic gem issues you can add `aggressive_keepalive: true` to your config. This should help resolve memory problems caused by newrelic gem. – Lenart Nov 28 '14 at 18:26
  • Thanks @Lenart, those are good suggestions. I downgraded to 2.0 when I was experiencing this issue, hopefully the 2.2 release will resolve the issue, although based on what I have read, I am not entirely convinced it will. I will update this post as soon as 2.2 is released. – Matthew O'Riordan Dec 01 '14 at 17:09
  • I am away at the moment, but will try in the New Year and provide an update here. – Matthew O'Riordan Dec 29 '14 at 21:32
  • @ari Ruby 2.2 is having the same issues for me – dylanjha Jan 05 '15 at 17:56
  • 1
    I've just upgraded my gems & deployed with Ruby 2.2, I will provide an update once I have left it to run for a few hours. – Matthew O'Riordan Jan 16 '15 at 01:05