0

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:

  1. Is this likely to cause the memory build-up?
  2. What would be a smarter way to do this, in that case?
Christoffer
  • 2,271
  • 3
  • 26
  • 57

1 Answers1

0

I remove almost all of these variables and it still didn't help. I will assume the application controller was not causing the memory problem.

Christoffer
  • 2,271
  • 3
  • 26
  • 57
  • Christoffer did you every find what was happening? I have the same issue, also on Heroku. If so, any tips or tools you would suggest? – unknown May 07 '21 at 11:25
  • To dig into memory issues, it is not so much about finding a magic bullet (well, I guess jemalloc is almost a magic bullet, google it) but rather go through the code over an over, gain new knowledge, improve the code etc. I use Scout on Heroku quite a bit, try to find the controllers that are slow/memory hungry, look through that code and read up on what could cause memory issues. It is a tedious work, don't expect to solve it right away - and good luck! – Christoffer May 10 '21 at 05:18