如何处理非 Image 对象上的 RMagick 泄漏?

发布于 2024-10-21 04:11:04 字数 689 浏览 5 评论 0原文

所以我知道 RMagick 会在 Image 对象中到处泄漏,并且 #destroy! 会释放该内存。

然而,我正在编写一个迭代图像所有像素的脚本,而且我必须处理这样一个事实:即使是 RMagick 的其他对象,包括 Pixel,也会到处泄漏。

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

运行并观察它泄漏得到的到处都是。只需读取像素即可。

如何在不疯狂泄漏的情况下迭代这些像素? 我对 RMagick 解决方案、非 RMagick 解决方案感兴趣,甚至对非 Ruby 解决方案(如果需要)感兴趣(但我真的不想) 。


如果已经有一个用于此特定任务的工具,但我无法找到搜索查询,这就是我正在做的事情:我有两个图像,它们是通过在纯色背景上叠加 RGBA 图像而生成的(在本例中为,一黑一白)。该脚本执行一些简单的数学运算来检索原始图像、Alpha 透明度等。我找不到命令行工具来完成这项确切的工作,但是,话又说回来,用一句话来表达并不容易。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-10-28 04:11:04

目前,我选择使用 ChunkyPNG,一个纯 Ruby 库,以避免 RMagick 的泄漏。我可能会在不久的将来将 C 扩展 OilyPNG 添加到该设置中。

我暂时接受这个答案,因为它解决了我的特殊问题。但是,如果有人找到真正解决非图像上 RMagick 内存泄漏核心问题的解决方案,请无论如何发布:D 谢谢!

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 :D Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文