Heroku Error R14 (Memory quota exceeded): How do I solve this?
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.
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.sizeend
Progress Bar:https://github.com/paul/progress_bar/issues
Heroku specifies a RAM limit per dyno in its Acceptable Use Policy.
You could use memprof, specifically Memprof::Middleware
, to help find out what's allocating all the memory in your app during requests. This is really only suitable for use in a non-production environment, since it'll slow down your app.
It's common for a Rails app to be allocating many ActiveRecord objects, so you might look for places where you're doing Model.find(:all).select{...}
, or similar queries that pull a lot of records from the DB and then process them in Ruby. Better would be to use where
to limit what is returned to the Rails app using SQL.