图像和堆大小问题

时间:2013-04-12 05:57:39

标签: java image graphics bufferedimage

我正在尝试使用Asprise裁剪应用裁剪1000张图片。

该过程首先从Content Manager下载图像,然后裁剪图像,然后再次在Content Manager中上传裁剪后的图像。

我创建了批处理文件来运行1000张图片, 最初它适用于300张图片,并给出了以下错误

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.awt.image.DataBufferInt.<init>(Unknown Source)
        at java.awt.image.Raster.createPackedRaster(Unknown Source)
        at java.awt.image.DirectColorModel.createCompatibleWritableRaster(
n Source)
        at java.awt.image.BufferedImage.<init>(Unknown Source)
        at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
        at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
        at sun.java2d.loops.MaskBlit$General.MaskBlit(Unknown Source)
        at sun.java2d.loops.Blit$GeneralMaskBlit.Blit(Unknown Source)
        at sun.java2d.pipe.DrawImage.blitSurfaceData(Unknown Source)
        at sun.java2d.pipe.DrawImage.renderImageCopy(Unknown Source)
        at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
        at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
        at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
        at sun.awt.image.ImageRepresentation.drawToBufImage(Unknown Source
        at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
        at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
        at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
        at sun.java2d.SunGraphics2D.drawImage(Unknown Source)

然后我将堆大小增加到-Xmx728M,为500 +图像工作,再次出现相同的outofMemory错误。

它会在标记的行

上抛出错误
  private BufferedImage toBufferedImage(Image image) {
         if (image instanceof BufferedImage) {
             return (BufferedImage)image;
         }

         image = new ImageIcon(image).getImage();

             boolean hasAlpha = hasAlpha(image);

         BufferedImage bimage = null;
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         try {
             int transparency = Transparency.OPAQUE;
             if (hasAlpha) {
                 transparency = Transparency.BITMASK;
             }

             GraphicsDevice gs = ge.getDefaultScreenDevice();
             GraphicsConfiguration gc = gs.getDefaultConfiguration();
             bimage = gc.createCompatibleImage(
                 image.getWidth(null), image.getHeight(null), transparency);
         } catch (HeadlessException e) {
             // The system does not have a screen
             e.printStackTrace();
         }

         if (bimage == null) {
             // Create a buffered image using the default color model
             int type = BufferedImage.TYPE_INT_RGB;
             if (hasAlpha) {
                 type = BufferedImage.TYPE_INT_ARGB;
             }
             bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
         }


         Graphics g = bimage.createGraphics();

         g.drawImage(image, 0, 0, null);      //error
         g.dispose();

      return bimage;
     }

我该怎么做才能解决这个问题? 如何释放BufferedImage内存?

我用过

BufferedImageObj.flush();
BufferedImageObj=null;

但它不起作用。

1 个答案:

答案 0 :(得分:2)

我怀疑你的错误实际上不是那条线的结果;而我怀疑绘制图像会将程序推入OutOfMemory状态。

您通常无法同时在内存中保存那么多图像。请记住,默认情况下,Java以全分辨率加载图像。我不认为它容易容纳数百张图像。确保您只在特定时刻加载您需要的那些。然后,让JVM垃圾收集内存,方法是在不再需要时将图像设置为null。