doremi
doremi

Reputation: 15339

Does memoizing an instance of a class in a class method cause a memory leak in Ruby?

My colleagues and I are in the midst of hunting down a memory leak in our Ruby on Rails application and we have some initial indicators that code resembling the following may be the culprit:

module Foo
  class Bar
    def self.example
      @widgets ||= ::Widget::Factory.new
    end
  end
end

Our hunch is that because self.example is a class method, it may not be properly garbage collecting the memoized instance of Widget::Factory, which utilizes extensive caching itself.

This, we believe, is leading to a memory leak every time one of our background workers is run. It seems to be spinning up an instance of Foo::Bar but never deallocating the objects created by Widget::Factory.

Does anyone have any insight into how MRI's GC would work for such a use-case or is this just a red-herring?

Upvotes: 2

Views: 692

Answers (0)

Related Questions