3

I have a following class:


class Myclass < ActionController::Metal
  def myaction
    huge_object.do_something
    # etc.
  end

  private
  def huge_object
    @obj ||= begin
      # allocate and initialize a ~10MB object
    end
  end
end

What I was amazed to find, is that even in production, each time the myaction is called, the object is allocated. Moreover, something is holding a reference to it, as the GC doesn't collect it. I made a workaround by wrapping the object in other class and including Singleton in that class. Still, I want to understand, what's going on here.

Roman
  • 13,100
  • 2
  • 47
  • 63

1 Answers1

0

This was mentioned in a rails comment on github issues I think not long ago, you can search for it there. Long story short, it does this because the class variable does not stick around between requests.

MatthewFord
  • 2,918
  • 2
  • 21
  • 32