Rails 4 Error R14 on Heroku (Memory Quota Exceeded)

20,516

Solution 1

If Papertrail is causing a problem, try a different add on. I've been using LogEntries without much of a problem. https://addons.heroku.com/#logging

Also try to lower your Unicorn worker processes so it uses lower total memory. Instead of the default of 3 (per box/dyno), try 2.

https://devcenter.heroku.com/articles/rails-unicorn#unicorn-worker-processes

You can also run a memory profiler on your app:

http://timetobleed.com/memprof-a-ruby-level-memory-profiler/

https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=profile

Solution 2

Digital Ocean wrote a great article about this.

Killing Unicorns can work in certain situations... however some people may have ethical concerns with this approach.

Solution 3

If you're using Rails 4 with Ruby 2.1, I bet the only change you need to make is downgrading to Ruby 2.0 back.

I spent 2 days to figure it out and I came up with this idea with Heroku support's suggestions. I think that's a bug or something like that with Ruby 2.1 or Rails 4.

Update by April 2015:

At the time, downgrading did work as a solution. But well, it's really not a recommended way of solving problems... Some people say upgrading to Ruby 2.2.0 with Rails 4.2 significantly decreases memory consumption. I would give it a try!

Share:
20,516
thecrentist
Author by

thecrentist

Updated on November 22, 2020

Comments

  • thecrentist
    thecrentist over 3 years

    I have been getting the (apparently common) Error R14, not sure when it started but noticed after installing the Papertrail add-on. I also added oink after seeing others mentioning it. I have tried everything others have tried in other SO questions but I can't seem to find the problem / memory leak.

    Error R14 (Memory quota exceeded)
    heroku/web.1: Process running mem=587M(114.7%)
    

    When I see this, to temporarily fix this, I run heroku restart which works for a while but I want to fix this problem permanently.

    From what I can see in Papertrail, the app just keeps loading the same page (home page) over and over again (and its not user traffic):

    app/web.1: Completed 200 OK in 436ms (Views: 45.5ms | ActiveRecord: 386.2ms) 
    app/web.1: May 25 18:14:52 5d2105e1-d799-4496-a2af-3785e78998db rails[9]: Oink Action: static_pages#home
    app/web.1: May 25 18:14:52 5d2105e1-d799-4496-a2af-3785e78998db rails[9]: Memory usage: 378860 | PID: 19
    app/web.1: May 25 18:14:52 5d2105e1-d799-4496-a2af-3785e78998db rails[9]: Oink Log Entry Complete 
    heroku/web.1: source=web.1 dyno=heroku.25566769.9d9a3da0-db4c-4b51-bc53-b69be9e43cb7 sample#memory_total=209.86MB sample#memory_rss=209.83MB sample#memory_cache=0.02MB sample#memory_swap=0.00MB sample#memory_pgpgin=59278pages sample#memory_pgpgout=5555pages 
    

    and after 2 minutes:

    heroku/web.1: source=web.1 dyno=heroku.25566769.9d9a3da0-db4c-4b51-bc53-b69be9e43cb7 sample#memory_total=293.73MB sample#memory_rss=291.94MB sample#memory_cache=0.02MB sample#memory_swap=1.77MB sample#memory_pgpgin=80890pages sample#memory_pgpgout=6147pages `
    

    I also enabled heroku labs:enable log-runtime-metrics from instructions from another question here and from Heroku here

    I have also added the New Relic add-on and this is what I see in the Monitoring > Instances tab:

    For the Last 7 Days: last 7 days

    For the Last 24 Hours: last 24 hours

    I am confused as you can see after 5/24 the memory usage went down and has since stayed down, but in the logs it shows memory being used mem=587M(114.7%) and in the last 24 hours an average of 150M, so I don't understand what is happening. I hope someone can help. Thanks.

    Update 1: Added Gemfile

    #Gemfile
    source 'https://rubygems.org'
    ruby '2.1.1'
    gem 'rails', '4.1.1'
    
    gem 'puma', '~> 2.8.2'
    gem 'turbolinks', '~> 2.2.2'
    gem 'pg', '~> 0.17.1'
    gem 'rack-attack', '~> 4.0.1'
    gem 'memcachier'
    gem 'dalli', '~> 2.7.1'
    gem 'oink'
    gem 'fog', '~> 1.22.0'
    gem 'activeadmin', github: 'gregbell/active_admin'
    gem 'dynamic_sitemaps', '~> 2.0.0'
    gem 'whenever', :require => false
    gem 'cancancan', '~> 1.8.0'
    gem 'mini_magick', '~> 3.7.0'
    gem 'carrierwave', '~> 0.10.0'
    gem 'devise', '~> 3.2.4'
    gem 'rolify', '~> 3.4.0'
    gem 'simple_form', '~> 3.0.2'
    gem 'cocoon', '~> 1.2.6'
    gem 'friendly_id', '~> 5.0.3'
    gem 'nokogiri', '~> 1.6.2.1'
    gem 'kaminari'
    gem 'impressionist', '~> 1.5.1'
    gem 'validate_url'
    gem 'searchkick', '~> 0.7.5'
    gem 'meta-tags', '~> 2.0.0'
    gem 'newrelic_rpm'
    
    
    group :assets do
      gem 'sass-rails', '~> 4.0.3'
      gem 'bootstrap-sass', '~> 3.1.1.1'
      gem 'uglifier', '~> 2.5.0'
      gem 'coffee-rails', '~> 4.0.1'
      gem 'asset_sync'
      #gem 'jquery-turbolinks'
      gem 'jquery-rails'
      gem 'jbuilder', '~> 2.0.7'
    end
    
    group :production do
      gem 'rails_12factor'
    end
    

    Update 2: Changed server from Puma to Unicorn

    It was pretty simple, following the directions here. And this is what it looks like now, which seems good, but I suspect it was because the application was restarted: last30mins

    # after heroku restart
    heroku/web.1: source=web.1 dyno=heroku.25566769.3770e52a-ddf3-485a-a88a-ac4fd54ab640 sample#memory_total=123.62MB
    
    # after 4 minutes
    heroku/web.1: source=web.1 dyno=heroku.25566769.3770e52a-ddf3-485a-a88a-ac4fd54ab640 sample#memory_total=276.48MB
    

    Update 3: Lowered the amount of Unicorn workers to 2

    After doing that, this is the average memory usage per dyno/instance for the past 6 hours according to New Relic: lasy6hoursafter

    And the output in PaperTrain (and also tried LogEntries) was this:

    app/web.1: May 26 19:54:08 21ae35ee-5656-4254-9f12-5dc6bb59efa1 rails[6]: Memory usage: 426076 | PID: 6
    app/web.1: May 26 19:54:08 21ae35ee-5656-4254-9f12-5dc6bb59efa1 rails[6]: Oink Log Entry Complete
    heroku/web.1: source=web.1 dyno=heroku.25566769.21ae35ee-5656-4254-9f12-5dc6bb59efa1 sample#load_avg_1m=0.00 sample#load_avg_5m=0.00 sample#load_avg_15m=0.00
    heroku/web.1: source=web.1 dyno=heroku.25566769.21ae35ee-5656-4254-9f12-5dc6bb59efa1 sample#memory_total=431.99MB sample#memory_rss=406.85MB sample#memory_cache=0.10MB sample#memory_swap=25.04MB sample#memory_pgpgin=198612pages sample#memory_pgpgout=94432pages 
    

    I checked the logs, and after 1 hour I was getting the R14 error and now it has been been going up very little, almost holding steady at 432MB and have not gotten the error anymore. So this seems to have fixed the problem! I will update over time if this changes.

  • Mike Szyndel
    Mike Szyndel almost 10 years
    I used Papertrail on heroku with Unicorn for a long time and had no problem with it. Strange anyway.
  • Abs
    Abs over 9 years
    What really worked for me was lowering the worker processes to 2
  • SteveO7
    SteveO7 over 9 years
    Downgrading to Ruby 2.0 fixed the memory quota errors, but I found my app ran noticeably slower. I upgraded to Ruby 2.1.3 and the R14 errors have not returned. You can turn on memory stats in the logs using log-runtime-metrics. Based on those log entries, my average memory usage dropped by about 100 megs over Ruby 2.1.2.
  • CoderDave
    CoderDave over 9 years
    I'm still getting R14 errors when upgrading to 2.1.3. Had to downgrade to 2.0.0. Using Heroku and Unicorn.
  • UserInteractive
    UserInteractive over 9 years
    Downgrading to Ruby 2.0 seems to be working for me. Had the problem with 2.1.3 and 2.1.5.
  • Graham Ashton
    Graham Ashton over 9 years
    I've had this problem since 18 November 2014, but I upgraded from Ruby 2.0 to Ruby 2.1 (version 2.1.3 to be precise) on 9 September 2014. Ruby 2.1 is a lot faster than 2.0 was though. I see R14 messages occurring after a restart after a couple of hours of typical usage on my app, am running two unicorns (I used to run three comfortably on Ruby 2.0) and am still on Rails 4.0. Are people also seeing this with Rails 4.1 or 4.2?
  • SteveO7
    SteveO7 over 9 years
    For what it's worth, I just upgraded to Ruby 2.2.0 and my Heroku memory consumption dropped significantly from Ruby 2.1.3. I'm using Rails 4.2.0
  • sguha
    sguha about 9 years
    How do you "lowering the worker processes"? Does that mean setting the WEB_CONCURRENCY environment variable?
  • Kyletns
    Kyletns about 9 years
    @sguha just check config/unicorn.rb, mine defaulted with worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) meaning that it looks for the WEB_CONCURRENCY env variable, then defaults to 3 if it doesn't find it.
  • Rafael Oliveira
    Rafael Oliveira over 8 years
    Puma running on ruby 2.2.3 with rails 4.0.13, problem persists... I'll try to upgrade to 4.2
  • xentek
    xentek over 8 years
    Papertrail doesn't interact with your app nor add any load to it. It interacts with your logs via what Heroku calls a "log drain".
  • Paniko0
    Paniko0 over 7 years
    kudos for you. Upgrading from 2.1.5 to 2.3.1 solved my worker memory consumption problem.