应用程序控制器中的变量会导致 Rails 中的内存泄漏吗? [英] Will variables in application controller cause a memory leak in Rails?

查看:54
本文介绍了应用程序控制器中的变量会导致 Rails 中的内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Heroku 上运行的 Rails 应用程序(每天大约 1000 次页面浏览).自上周启动以来,我一直在经历应用程序的频繁崩溃.

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.

查看 New Relic,似乎 Dynos 内存使用量不断增加,而内存使用量从未下降.基本上,它会在几个小时内累积,然后以请求超时结束,这似乎很可能.

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.

我的应用程序 (presenttips . com) 是一个礼物网站,其中包含随机礼物"、每日礼物"和横幅"等功能.这些我像这样加载到应用程序控制器中:

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.

我的问题:

  1. 这是否可能导致内存堆积?
  2. 在这种情况下,更聪明的方法是什么?

推荐答案

我删除了几乎所有这些变量,但仍然没有帮助.我会假设应用程序控制器没有导致内存问题.

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.

这篇关于应用程序控制器中的变量会导致 Rails 中的内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆