JMagick的JNI-hazard问题

文章探讨了使用JMagick通过JNI技术调用ImageMagick时存在的JNI-hazard问题,可能导致JVM崩溃。推荐使用IM4JAVA作为替代方案,以避免此类风险。讨论了JMagick和IM4JAVA之间的兼容性问题,并提供了相关官方文档解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JNI-hazard问题
JMagick使用JNI技术调用ImageMagick,存在JNI-hazard问题。不适合在长时间运行的Web应用程序使用ImageMagick +Jmagick,可能导致JVM崩溃。

我就在项目中遇到过,Tomcat在跑了数天后会crash,异常内容为:magick.MagickException: Unable to retrieve handle 。

从表象看,应该是没有释放ImageMagick的句柄导致的,查找API后发现程序中没有调用:magick.MagickImage.destroyImages() Called by finalize to deallocate the image handle。 在程序加上本方法,情况有所改善,大概在十多天后出现了一次tomcat进程crash。并不能根本解决问题。
建议使用IM4JAVA。

在JMagick的邮件列表中找到一份我认为有价值的邮件

The "JNI hazard" here is that if something you use (f.ex libtiff for reading  
TIFF files) has a memory bug then it can make your whole JVM crash. Thats of  
course frustrating and therefore its great to have im4java around, which  
starts IM as an external process, so JVM crashes are avoided.  
 * *  
Coolest thing would be if JMagick and im4java could have the same API so it  
was easy to switch depending on luckyness. Ive asked the author of im4java  
to attemt to be as compatible as possible, but as im4java is very much  
different internally its limited how much can be done in that direction.  
  
If you don't like the risk, stick to im4java. If your want optimal  
performance give JMagick a try.  
  
And, its not JMagick that is buggy, its what it depends on (hereunder IM)  
that is not always (memory) bug free on long running processes.  
I also have never seen a mismatch between JMagick binary and ImageMagick  
binaries leading to crashes. 

IM4JAVA官方文档也有相关说明:

1.1. What exactly do you mean with JNI-hazard? 
Running native code using JNI from within java always imposes additional risks. The JVM is no longer sandboxed, so there might be some security issues. In addition, there could be errors like memory leaks or memory corruption within the JNI-layer (JMagick) or within the native code (ImageMagick). 
This is especially dangerous for long running processes (typically web-application-servers). Memory corruption or a segmentation fault (maybe triggered by a intentionally manipulated image) might bring down the whole server. 
On the other side, in reality the situation is not as bad as it sounds above. JMagick is well tested, and for standard use-cases it prooves to be very stable. 
Some additional information is available on the JMagick-users mailing list where this topic has been discussed to some detail. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值