Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with memory leaks in RMagick in Ruby?

Im developing web-application with Merb and im looking for some safe and stable image processing library. I used to work with Imagick in php, then moved to ruby and start using RMagick. But there is a problem. Long running scripts causing memory leaks. There are couple solution exists, but I don't know which one is the most stable. So, what do you think?

Right now, my app uses internal API that i wrote to process images, in PHP. Its running on separate server along with other applications, so its not a big problem. But i think its not a good architecture.

Anyway, i`ll consider any practical tips.

like image 534
Dan Sosedoff Avatar asked Jun 06 '09 00:06

Dan Sosedoff


People also ask

What is memory leak in Ruby?

Memory leak happens when “memory which is no longer needed is not released”. In other words, running code loses control over allocated memory. Nothing leaks when the code of “Simple leak” is over or the variable runs out its scope of visibility.

How does Ruby manage memory?

For Dynamic Memory allocation, the Ruby program uses Heap memory and the basic unit of the heap is a slot. Here, each slot occupies a value which is known as RVALUE. This RVALUE comprises 40 bytes and a container for objects of all types (Array, String, Class).


1 Answers

I too have encountered this issue - the solution is to force garbage collection.

When you have reassigned the image variable to a new image simply use GC.start to ensure the old reference is released from memory.

On later versions of RMagick, I also believe you can also call destroy! on the image when you have finished processing it.

A combination of the two would probably ensure you are covered, but im not sure of the real life impact on performance (I would assume it is negligible i most cases).

Alternatively, you could use mini-magick which is a wrapper for the ImageMagick commandline client.

like image 89
Matt Avatar answered Sep 24 '22 00:09

Matt