#356 ✓resolved
Sam Carr

Memory leak: @transactions is keeping Thread refs

Reported by Sam Carr | June 7th, 2008 @ 06:20 AM

I've found a nasty memory leak and I've also found how to fix it, but I need to bring it up for discussion it as my fix is probably not so good.

First of all, the problem:

dm-core/lib/data_mapper/adapaters.rb: initialize defines @transactions = Hash.new { |hash, key| hash[key] = [] }

However current_transaction uses @transactions[Thread.current].last

So we end up storing references to Threads in @transactions. I'm using DM in Ramaze, which uses a separate thread per request, and so each time I do something with DM in my controller, the thread gets added to @transactions, where it stays forever, along with all the other data attached to it. Hence I see a leak of about 20Kb per request in my case. Which is pretty bad.

The solution:

Changing initialize fixes the leak completely:

@transactions = Hash.new { |hash, key| [] }

This doesn't break anything obvious in my usage of DM but I don't know the DM code at all well so it may have some negative impact that I'm not aware of and I may be missing some important subtlety.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket