RMagick and memory leaks

Published on 04/04/09

Last night I upgraded the slice that Net-at-hand is running on because my mongrels kept leaking memory. I was running four of them at the time and I would invariably get one or two that would spike to over 140MB and that would bog the whole system down.

I knew that RMagick had something to do with it, but I was dutifully calling GC.start whenever I used it for anything. So I figured that it would all work out in the end.

Well, this morning I checked on my mongrels (I am now running six of them) and they were all humming along fine, each taking up about 90MB of ram. I decided to try an image upload to see how it would work. After uploading a 6MB image one of the mongrels spiked to 150MB and stayed there. That was not good!! If two or three users uploaded an image at the same time, I would end up going into swap memory usage again, and I couldn’t have that.

After some more googling I found this article that introduced me to the destroy! method that can be called on images. When you are done with an image you can destroy it and the memory used by it is released.

I tried calling it on the RMagick::ImageList first but it did not help. I ended up calling it on each image and that fixed the problem.

I had done a bunch of googling for rmagick memory leaks and hadn’t found much (admittedly I did not spend hours looking through the results list). Hopefully, this will help you if you are having the same trouble I was.

UPDATE
I am really excited about the results of this fix. It is working flawlessly. Just as an example, I set up the system to process a 68MB image. When I did, the mongrel that was handling it climbed to over 200MB of ram usage. But it dropped right back down to normal. GC.start was not completely fixing problems like this on my system. I am not sure why, because from what I understand, it should have. But now the problem is gone. Praise the Lord!

Things seem to be going so well, actually, that I could probably downgrade back to the smaller slice, but I am not going to. With the introduction of a plugin system on Net-at-hand I don’t want to take any chances.

Comments

Leave your comment: