连续运行程序,内存泄漏

时间:2012-06-20 22:57:00

标签: ruby memory-leaks

我想无限期地运行一个脚本。它看起来像我当前的脚本,如下所示,导致内存泄漏。另外我想保留@time_value数组,因为我需要循环每次新运行的值。

  class Data

    def initialize
      first_method
      @time_value = [30, 60, 90]
    end

    def first_method

      # get some data; takes about 1 hour
      second_method
    end

    def second_method
      #process the data
      first_method
    end
  end

  d = Data.new

1 个答案:

答案 0 :(得分:2)

永久递归不仅可能继续消耗内存,而且最终会因堆栈溢出而失败。将您的无限期处理更改为一个简单的循环。

class Data
  def initialize
    @time_value = [30, 60, 90]
  end

  def run_forever
    while true
      first_method
      second_method
    end
  end

  def first_method
    # get some data; takes about 1 hour
  end

  def second_method
    #process the data
  end
end

Data.new.run_forever