views:

193

answers:

1

I'm having problems with a long-lived background ruby process on our server, which isn't cleaning up Tempfiles. I'm using hijack to inject into the process & inspect things, using, for example,

ObjectSpace.each_object(ActiveRecord::Base){|o| puts o}

- turns out that the Tempfiles in question are being referenced by an instance of one of our ActiveRecord subclasses, and those instances aren't being collected.

I haven't been able to figure out what's referencing those AR instances & keeping them alive. Any tips for getting access to whatever object graph the garbage collector uses?

+1  A: 

Ruby's garbage collector is a mark & sweep algorithm: (1) start from the Object instance and then walk through every reachable object marking them along the way (2) then walk the ObjectSpace for every object reference and delete those that are not marked.

Here is some reading on the subject of debugging GC memory problems: http://blog.evanweaver.com/articles/2009/04/09/ruby-gc-tuning/

The only things in your rails app that are long lived are the classes and modules. With that in mind some places to look are:

1) are these active record instances being squirreled away in a class variable 2) somehow being cached by rack middleware 3) being held onto by the active record database connection pool 4) are you using ruby finalizers (notorious for memory leaks when used incorrectly) see eigenclass.org/hiki/deferred-finalizers-in-Ruby

Sorry that I'm just posting a bunch of ideas and few solutions. Hope this gets you thinking in some new directions.

Blessings, TwP

TwP