[PJUG Javamail] JNI vs Runtime

Wayne Carstensen waynec at c-ware.com
Fri Apr 9 20:05:03 UTC 2010


Let me state, this was NOT my first choice by any stretch! I tend to be 
a purist/fundamentalist. I first tried JAI, but had little luck with 
quality and by the time we approached the quality that we wanted, both 
performance and memory usage were horrible. We actually felt we had less 
control over either of these factors.

Next, we tried the JNI approach, but from reading and practical 
implementation, we still had memory concerns. Also, this was over four 
years ago and the API was also lacking. Similar to the way documentation 
has a hard time keeping up with changes to an API, so do JNI wrappers.

Finally, it was just easier, faster and complete to (begrudgingly) call 
exec(). It was nice because there were no mem leaks or gc() issues. In 
our case the image operations were finite, which means that we had a 
source image, a list of operations to perform (crop, rotate, scale, 
watermark and brand) and a target location. When the operation was 
complete, it was guaranteed there would be no lingering resources. 
Granted there is cost of allocating those resources each time, but since 
we were maintaining a pool of cached results, we knew access would be 
minimal and diminishing.

Of course, a good design buries such details, and bury we did (deep). We 
created an image service with a simple RMI interface. We could then send 
images to the service for persistence and also recall the image with any 
number of image operations. The service implementation uses the 
underlying OS to store the images and an SQL table to store meta 
details. The OS is also used to cache the requests to avoid duplicate 
work, returning prior results immediately. As a service we could install 
it on our fastest server.

Since our design included caching results it minimized the performance 
impact, so it ultimately came down to quality (customer driven 
requirement of course). It was quite the challenge to turn 6 Mpx photos 
into 32x32 icons. Hours of research was spent trying to accomplish in 
JAI which is well perfected in ImageMagick. And although we use exec() 
to manipulate the images, we never threw away our JAI implementation if 
at some time we discover the secret formula for thumbnails.

- Wayne

Richard Johnson wrote:
> Hi all.  Happy Friday.
> 
> Wayne Carstensen said:
> " If you decide to try ImageMagic, skip the JNI bridge technique
> " and go straight to using Runtime.exec() calls.
> 
> Would anyone care to characterize this as a good idiom in general, to
> simply skip JNI in favor of asking the system to do it?  What are the
> tradeoffs?




More information about the Javamail mailing list