Ruby on Rails memory leak when looping through large number of records; find_each doesn't help

前端 未结 3 996
醉酒成梦
醉酒成梦 2020-12-28 14:08

I have a Rails app that processes a large (millions) number of records in a mysql database. Once it starts working, its memory use quickly grows at a speed of 50MB per secon

3条回答
  •  情话喂你
    2020-12-28 14:53

    I was able to figure this out myself. There are two places to change.

    First, disable IdentityMap. In config/application.rb

    config.active_record.identity_map = false
    

    Second, use uncached to wrap up the loop

    class MemoryTestController < ApplicationController
      def go
        ActiveRecord::Base.uncached do
          Person.find_each do |person|
            # whatever operation
          end
        end
      end
    end
    

    Now my memory use is under control. Hope this helps other people.

提交回复
热议问题