From: Jamis Buck Date: 2005-10-18T02:33:27+09:00 Subject: Re: Help! define_method leaking procs... On Oct 17, 2005, at 11:07 AM, Eric Mahurin wrote: > --- Jamis Buck wrote: > >> On Oct 16, 2005, at 6:57 PM, Lyndon Samson wrote: >> We have identified and fixed the leak, and all is well now. >> Thanks, >> everyone, for your suggestions. >> >> If you're curious, the problem was fixed by calling >> undef_method on >> all the dynamically added methods before doing remove_const >> on the >> class. We also went through each class and removed all >> instance_variables from the class. Doing this seems to have >> allowed >> the Proc objects to be garbage collected. >> > > Would you mind showing us a snippet of code that demonstrates > the problem? Maybe there is some circular references? Well... here's a small script that duplicates the problem, but it requires that it be run from the root directory of a Rails application that has a model class called Dummy. And Dummy must have an association to some other model class. require './config/environment' module ObjectSpace def self.count(mod=Object) count = 0 ObjectSpace.each_object(mod) { count += 1 } count end end 20.times do GC.start Dummy.find(1) Dependencies.clear ActiveRecord::Base.reset_subclasses Dependencies.remove_subclasses_for(ActiveRecord::Base) procs = ObjectSpace.count(Proc) puts "procs: #{procs}" end The set it up for this, you could do: * gem install rails * rails memleak * cd memleak * script/generate model Dummy * script/generate model Thing * edit Dummy so that it has_one :thing * edit config/database.yml to point to a database * add a dummies table * insert a record into the dummies table * run the script given above Running against Rails 0.13.1, you should see the number of procs grow with each request. Running against the beta gems, you'll see the number remain constant. - Jamis