heroku - Rails memory bloat caused by too many records loaded? What can I do? -
i have views in heroku app load multiple records has_many associations within has_many associations.
some requests taking on 50mb of dyno memory (leading out of memory errors) , i'm not sure best way resolve problem.
i using pagination cut down on records loaded.
@items = @section.items.order(priority: :desc).paginate(page: params[:page], :per_page => 20)
inside view, looping through associations display data.
@items.sizes.where(active: true).order(priority: :desc).each .. end @items.addons.where(active: true).order(priority: :desc).each .. end @items.addons.sides.where(active: true).order(priority: :desc).each .. end
on big page, loading 20+20*3+20*4+20*4*20 = 1760 records
assuming don't mind slow speed, reason why each request taking memory? , if so, doesn't garbage collector clean up? in heroku metrics, see memory bloat increasing..
you sort of answered own question there mate :) why should keep type of logic in controller using includes
well, include items of associated records in 1 query instead of several separate queries.
so in view call @items.each do
@items
being defined in controller.
if understand correctly – 3 arrays have there in view, 3 different things needed looped , shown separately?
i hate types of answers link bunch of tutorials, issues result of many different things. so, without further due:
this great article on how achieve need lot of detailed, yet easy grasp explanations.
here's article debugging memory bloat.
i hope can @ least.
Comments
Post a Comment