Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-500

Memory Leak using Threads

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.8.1
    • Affects Version/s: 1.7.0
    • Component/s: None
    • Environment:
      CentOS 5.4, ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]

      Consider the following Ruby code:

      require 'mongo'
      DB = Mongo::Connection.new('localhost', 27017)['test_db']
      @collection = DB['test_collection']

      code = -> { @collection.find(

      {"Name" => "aaron.symplicity.com"}

      ).each

      {|d| d}

      }
      high = 0
      pid = 0
      size = 0

      loop do
      th = Thread.new do
      code.call
      pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#

      {Process::pid}

      "`.chomp.split(/\s+/).map

      {|s| s.strip.to_i}

      if size > high
      puts "New high: #

      {size}

      "
      high = size
      p "Threads: #{ObjectSpace.each_object(Thread) {}}"
      p "Strings: #{ObjectSpace.each_object(String) {}}"
      end
      end
      th.join
      end

      If you look at the output of this code, Thread objects are never garbage collected after the thread does a query using the MongoDB driver. If you comment out "code.call", thread objects are garbage collected and usage stays down. Try inserting a few objects with string values into the test db/collection and running the above code, and you'll see a steady increase in string objects.

      This pretty much makes Mongo + EventMachine.defer (or any thread pool) a no go.

      Also a note, this does not happen with Fiber's – only Threads.

            Assignee:
            tyler@10gen.com Tyler Brock
            Reporter:
            cidan Antonio Lobato
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: