I have an application in Rails that I run on Heroku (apprx 1 000 page views a day). I have been experiencing frequent crashes of the application since the launch last week.
Looking into New Relic it seems like the Dynos memory usage is constantly increasing without ever going down in memory usage. Basically, it builds up during a couple of hours and then end in request timeouts, which seems likely.
Thus, I believe the problem with the app crashing is due to a memory leak.
My app (presenttips . com) is a gift website where I have features like "random gift", "gift of the day" and "banners". These I load in the application controller like this:
before_filter :global_setup
def global_setup
# Create random gift
rand_gift = []
rand_gift << Gift.where(:gift_status_id => 1) #=> Accepted
@random_gift = rand_gift[0][rand(rand_gift[0].size) - 1]
rand_gift = nil
@nbr_of_active_gifts = (Gift.where(:gift_status_id => 1).count / 100 ).round * 100
@toplist = Gift.where(:gift_status_id => 1).order("week_click DESC").limit(20)
@banners = Banner.where("first_date <= '" + Time.now.to_date.to_s + "'").where("last_date >= '" + Time.now.to_date.to_s + "'").order("first_date ASC")
advertise_here = []
(@banners.count..4).each do |i|
advertise_here[i] = Banner.new(:advertiser => "Presenttips.com", :banner_image => "annons.jpg", :url => advertise_path)
end
@banners << advertise_here.compact
@banners = @banners.flatten
@page_categories = PageCategory.order(:prio_rank)
if Rails.env.production?
@random_sql = "RANDOM()"
@meta_robots_block = false
@analytics_block = false
else
@meta_robots_block = true
@analytics_block = true
@random_sql = "RAND()"
end
gift_from_daily = DailyGift.where(:publish_date => Time.now.to_date).first
gift_from_daily = DailyGift.create(:publish_date => Time.now.to_date, :gift_id => @random_gift.id) if gift_from_daily.blank?
@daily_gift = Gift.find(gift_from_daily.gift_id)
@head_categories = Category.order(:name).where(:parent_id => nil)
todays_date = Time.now.to_date.to_s
@season = Season.where("'" + todays_date + "' >= date_start ", "'" + todays_date + "' <= date_end" ).first
@season_theme = @season.css
@logo = 'logo.png'
@logo = 'seasons/logo_christmas.png' if @season.css.eql?('theme_christmas.css')
end
so that I can use them in the app globabally (gift of the day, for example, is always presenet in the right column).
I guess this is not great considering memory usage though.
My questions:
- Is this likely to cause the memory build-up?
- What would be a smarter way to do this, in that case?