jhuckabee
jhuckabee

Reputation: 66

Ruby garbage collection on anonymous classes using define_method

I'm trying to track down a memory leak in the state_machine gem that I began to notice when I started dynamically generating state machines using anonymous classes per the README example. This gist demonstrates how I uncovered the issue originally.

I've been able to narrow down the problem to the way helper methods get defined in the state machine gem which I've summarized in this gist.

My question is, what is the define_method block doing that causes the anonymous class not to get garbage collected? Commenting those 3 lines (18-20) seems to solve the issue.

Thanks in advance for any pointers.

Update: I'm using Ruby 2.0.0p195

Upvotes: 1

Views: 365

Answers (1)

jhuckabee
jhuckabee

Reputation: 66

This article explains the problem.

The main down side is that define_method creates a closure. The closure could hold references to large objects, and those large objects will never be garbage collected.

Upvotes: 1

Related Questions