gregbell/active_admin — pull request #1571 by TaiGat (2012-07-31 20:24:06+00:00)
Title: Temporary fix for memory leak (issue 170).

Hello.

I've been trying to figure this memory leak out for recent few days and I think I know what may cause it.

First, a small disclaimer. Since I'm just learning Rails I have not been testing it in a real application environment, but instead used memory_spec test.
I would appreciate it if someone could test it in a real Rails application's production environment.

Now, to the point. There are two resource dependencies that somehow cause GC not to sweep the Resource objects after their respective Controllers are unregistered:
1. Circular dependency between controller and its resource.
2. The ResourceDSL config instance variable when registering resource with a block (e.g. Comments). I think this might be caused by the closure nature of Ruby blocks.

To fix the first issue I had to reset the circular dependency in the controller after its constant is unregistered.
Unfortunately to fix the second issue I had to introduce another circular dependency between Resource and ResourceDSL instances in order to remove it later.
Both of these are implemented in private clear_resource_bindings method in Namespace class.

These fixes are far from perfect to say the least - they break encapsulation and introduce another unneeded dependency.
(Un)Fortunately I am leaving for vacation soon, so this is all the work I can do on it for now.

Hopefully someone with deeper AA understanding will have some better ideas on how to fix these two situations :)