ActiveRecord objects in hashes aren't garbage collected — a bug or a sort of caching feature?

前端 未结 2 470
有刺的猬
有刺的猬 2021-01-30 07:14

I have a simple ActiveRecord model called Student with 100 records in the table. I do the following in a rails console session:

ObjectSpace.each_obj         


        
2条回答
  •  野性不改
    2021-01-30 07:42

    I think I know what's going on. Ruby's GC wont free immutable objects (like symbols!). The keys returned by group_by are immutable strings, and so they wont be garbage collected.

    UPDATE:

    It seems like the problem is not with Rails itself. I tried using group_by alone, and sometimes the objects would not get garbage collected:

    oscardelben~/% irb
    irb(main):001:0> class Foo
    irb(main):002:1> end
    => nil
    irb(main):003:0> {"1" => Foo.new, "2" => Foo.new}
    => {"1"=>#, "2"=>#}
    irb(main):004:0> ObjectSpace.each_object(Foo).count
    => 2
    irb(main):005:0> GC.start
    => nil
    irb(main):006:0> ObjectSpace.each_object(Foo).count
    => 0
    irb(main):007:0> {"1" => Foo.new, "2" => Foo.new}.group_by
    => ##, "2"=>#}:group_by>
    irb(main):008:0> GC.start
    => nil
    irb(main):009:0> ObjectSpace.each_object(Foo).count
    => 2 # Not garbage collected
    irb(main):010:0> GC.start
    => nil
    irb(main):011:0> ObjectSpace.each_object(Foo).count
    => 0 # Garbage collected
    

    I've digged through the GC internals (which are surprisingly easy to understand), and this seems like a scope issue. Ruby walks through all the objects in the current scope and marks the ones which it thinks are still being used, after that it goes through all the objects in the heap and frees the ones which have not been marked.

    In this case I think the hash is still being marked even though it's out of scope. There are many reasons why this may happening. I'll keep investigating.

    UPDATE 2:

    I've found what's keeping references of objects. To do that I've used the ruby mass gem. It turns out that Active Record relation keeps track of the objects returned.

    User.limit(1).group_by(&:name)
    GC.start
    ObjectSpace.each_object(ActiveRecord::Base).each do |obj|
      p Mass.references obj # {"ActiveRecord::Relation#70247565268860"=>["@records"]}
    end
    

    Unfortunately, calling reset on the relation didn't seem to help, but hopefully this is enough information for now.

提交回复
热议问题