So I know that RMagick leaks all over the place with Image objects, and that #destroy! frees up that memory.
However, I’m writing a script that iterates through all the pixels of an image, and I have to deal with the fact that even RMagick’s other objects, including Pixel, leak all over.
image = Magick::Image::read('/path/to/image.png')[0]
5.times do
100000.times do
image.pixel_color(0,0)
end
print "Check memory, then press enter";gets
end
Run and watch it leak all over the place. Just by reading pixels.
How can I iterate over these pixels without leaking like crazy? I’m interested in RMagick solutions, non-RMagick solutions, and even non-Ruby solutions if need be (but I’d really rather not).
In case there’s already a tool for this specific task that I can’t find a search query for, here’s what I’m doing: I have two images that were generated by overlaying an RGBA image on a solid-color background (in this case, one black and one white). This script does some simple math to retrieve the original image, alpha transparency and all. I couldn’t find a command-line tool to do this exact job, but, then again, it’s not exactly easy to put into a short phrase.
For the time being, I’ve opted to use ChunkyPNG, a pure Ruby library, to avoid RMagick’s leakiness. I may add the C extension OilyPNG to that setup in the near future.
I’m accepting this answer for the time being, since it solves my particular problem. However, if anyone finds a solution that actually addresses the core issue of RMagick’s memory leaks on non-images, please post it anyway 😀 Thanks!