title: JVM crashes while image conversion using javax.media.jai
body: <p>In our app, we use javax.media.JAI library to scale and improve image</p>
  
  <p>But sometimes (1 out of 10 times), it is causing the VM to crash</p>
  
  <pre><code>
  #
  # A fatal error has been detected by the Java Runtime Environment:
  #
  #  SIGSEGV (0xb) at pc=0x00007fdb6f49140b, pid=2153, tid=140580255180544
  #
  # JRE version: 6.0_22-b22
  # Java VM: OpenJDK 64-Bit Server VM (20.0-b11 mixed mode linux-amd64 compressed oops)
  # Derivative: IcedTea6 1.10.4
  # Distribution: Fedora release 16 (Verne), package fedora-61.1.10.4.fc16-x86_64
  # Problematic frame:
  # C  [libgs.so+0x16940b]  s_DCT_set_defaults+0x8b
  #
  # An error report file with more information is saved as:
  # /usr/share/tomcat/hs_err_pid2153.log
  #
  # If you would like to submit a bug report, please include
  # instructions how to reproduce the bug and visit:
  #   http://icedtea.classpath.org/bugzilla
  # The crash happened outside the Java Virtual Machine in native code.
  # See problematic frame for where to report the bug.
  #
  </code></pre>
  
  <p>Adding logs to code did help to nail down the exact line </p>
  
  <pre><code>
                  DebugHelper.errorLog("Start transformImage2");
          List modImages = new ArrayList();
          PageImage page = new PageImage();
  
          // generate rendered image.
          RenderedOp im = JAI.create("fileload", image.getFile()
                  .getAbsolutePath());
  
          // get the width and height of original image.
          float width = im.getWidth();
          float height = im.getHeight();
  
          // improve the quality of image.
          DebugHelper.errorLog("Improving quality of image");
          RenderingHints qualityHints = new RenderingHints(
                  RenderingHints.KEY_RENDERING,
                  RenderingHints.VALUE_RENDER_QUALITY);
          qualityHints.put(RenderingHints.KEY_ANTIALIASING,
  </code></pre>   
  
  <p>"Start transformImage2" gets printed but "Improving quality of image" does not. So we know it is in those 3-4 lines. Additionally from hs_err* file it points to float width = im.getWidth();</p>
  
  <pre><code>
  Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
  j  com.sun.imageio.plugins.jpeg.JPEGImageReader.initJPEGImageReader()J+0
  j  com.sun.imageio.plugins.jpeg.JPEGImageReader.(Ljavax/imageio/spi/ImageReaderSpi;)V+174
  j  com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi.createReaderInstance(Ljava/lang/Object;)Ljavax/imageio/ImageReader;+5
  j  javax.imageio.spi.ImageReaderSpi.createReaderInstance()Ljavax/imageio/ImageReader;+2
  j  javax.imageio.ImageIO$ImageReaderIterator.next()Ljavax/imageio/ImageReader;+16
  j  javax.imageio.ImageIO$ImageReaderIterator.next()Ljava/lang/Object;+1
  j  com.sun.image.codec.jpeg.JPEGCodec$ImageIOJPEGImageDecoder.(Ljava/io/InputStream;)V+26
  j  com.sun.image.codec.jpeg.JPEGCodec$ImageIOJPEGImageDecoder.(Ljava/io/InputStream;Lcom/sun/image/codec/jpeg/JPEGCodec$1;)V+2
  j  com.sun.image.codec.jpeg.JPEGCodec.createJPEGDecoder(Ljava/io/InputStream;)Lcom/sun/image/codec/jpeg/JPEGImageDecoder;+6
  j  com.sun.media.jai.codecimpl.JPEGImage.(Ljava/io/InputStream;Lcom/sun/media/jai/codec/ImageDecodeParam;)V+35
  j  com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(I)Ljava/awt/image/RenderedImage;+29
  j  com.sun.media.jai.opimage.CodecRIFUtil.create(Ljava/lang/String;Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+184
  j  com.sun.media.jai.opimage.JPEGRIF.create(Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+4
  j  sun.reflect.GeneratedMethodAccessor740.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+48
  J  sun.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
  j  java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+161
  j  javax.media.jai.FactoryCache.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+6
  j  javax.media.jai.OperationRegistry.invokeFactory(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;+57
  j  javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;+12
  j  javax.media.jai.registry.RIFRegistry.create(Ljavax/media/jai/OperationRegistry;Ljava/lang/String;Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+35
  j  com.sun.media.jai.opimage.StreamRIF.create(Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+191
  j  sun.reflect.GeneratedMethodAccessor740.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+48
  J  sun.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
  j  java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+161
  j  javax.media.jai.FactoryCache.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+6
  j  javax.media.jai.OperationRegistry.invokeFactory(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;+57
  j  javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;+12
  j  javax.media.jai.registry.RIFRegistry.create(Ljavax/media/jai/OperationRegistry;Ljava/lang/String;Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+35
  j  com.sun.media.jai.opimage.FileLoadRIF.create(Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+158
  j  sun.reflect.GeneratedMethodAccessor740.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+48
  J  sun.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
  j  java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+161
  j  javax.media.jai.FactoryCache.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+6
  j  javax.media.jai.OperationRegistry.invokeFactory(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;+57
  j  javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;+12
  j  javax.media.jai.registry.RIFRegistry.create(Ljavax/media/jai/OperationRegistry;Ljava/lang/String;Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljava/awt/image/RenderedImage;+35
  j  javax.media.jai.RenderedOp.createInstance(Z)Ljavax/media/jai/PlanarImage;+171
  J  javax.media.jai.RenderedOp.createRendering()V
  j  javax.media.jai.RenderedOp.getWidth()I+1
  j  com.myapp.helpers.conversion.DocumentConverter.transformImage(Lcom/myapp/helpers/conversion/PageImage;Ljava/lang/String;Lcom/myapp/helpers/conversion/ImageSetting;)Ljava/util/List;+39
  j  com.myapp.helpers.conversion.DocumentConverter.convertDocuments(Ljava/util/List;Ljava/lang/String;Lcom/myapp/helpers/conversion/ImageSetting;)Ljava/util/Map;+597
  J  com.myapp.helpers.conversion.ConversionTask.run()V
  </code></pre>
  
  <p>So I have a few questions</p>
  
  <ol>
  <li><p>What is (can be) exact cause of the problem?</p></li>
  <li><p>Why does the error not occur all the time, same image gets converted successfully at times and fail at others?</p></li>
  <li><p>How to solve (ofcourse :) ? </p></li>
  </ol>