Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Created August 7, 2012 04:16

Revisions

  1. ryanlecompte created this gist Aug 7, 2012.
    19 changes: 19 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # It appears that when I perform a query with AR via multiple threads,
    # the instantiated objects do not get released when a GC is performed.

    threads = Array.new(5) { Thread.new { Foo.where(:status => 2).all.first(100).each { |f| f.owner.first_name } } }
    threads.each(&:join)
    threads = nil

    GC.start

    ObjectSpace.each_object(Foo).count # => instances still exist

    # ----------------

    Foo.where(:status => 2).all.first(100).each { |f| f.owner.first_name }

    GC.start

    ObjectSpace.each_object(Foo).count # => 0