Heroku Error R14 (Memory quota exceeded): How do I solve this?

I had this problem .. to solved using find_in_batches.

If someone still has this error i will put the code here. As it takes long time to run, i found a progress_bar gem that help the user. will let it here too cause i think its mandatory in almost every case.

bar = ProgressBar.new( total )
Texto.find_in_batches(:batch_size => 100) do |textos|
    textos.each{| texto | 
        ...do_stuff...
    }
    bar.increment! textos.size
end

Progress Bar: https://github.com/paul/progress_bar/issues


Look for code with 'Model.all.each do |something|' and replace with Model.find_each do |something|. This will save memory by loading chunks of your model into memory instead of the entire model all at once.

Also, look for opportunities to use in_groups_of or :limit to decrease the number of objects that are saved in memory at one time.

EDIT: for_each -> find_each.


Also, the WEB_CONCURRENCY heroku config can cause this. For example, I have an app running 2 1x dynos. If I use sensible_defaults (another heroku config), it defaults WEB_CONCURRENCY to 2 (based on my dynos). This causes a ton of R14 errors in my logs. If I turn off sensible_defaults, and set WEB_CONCURRENCY=1...the problem goes away.

If you don't set WEB_CONCURRENCY, and you don't use sensible_defaults, and you followed the server (Im using Puma) set up via heroku docs...you'll likely have a line in puma.rb that looks like this:

workers Integer(ENV['WEB_CONCURRENCY'] || 3)  

If thats the case, then WEB_CONCURRENCY isn't set...so it will be 3, which is using even more memory.

In console:

// review existing config settings.

heroku config

// IF sensible_defaults is enabled.

heroku config:set sensible_defaults=disabled

// set WEB_CONCURRENCY

heroku config:set WEB_CONCURRENCY=1

// monitor logs for R14 errors.