2

我正在尝试使用 Asprise 裁剪应用程序裁剪 1000 张图像。

该过程是,首先从内容管理器下载图像,然后裁剪图像,然后再次将裁剪后的图像上传到内容管理器。

我创建了批处理文件来为 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;

但它不起作用。

4

1 回答 1

2

我怀疑您的错误实际上不是该行的结果;相反,我怀疑绘制图像会将程序推入OutOfMemory状态。

您通常不能一次在内存中保存那么多图像。请记住,Java 默认以全分辨率加载图像。我不认为它很容易拥有数百张图像。确保您只在给定时刻加载您需要的那些。然后,当不再需要图像时,让 JVM 垃圾通过将图像设置为 null 来收集内存。

于 2013-04-12T06:00:42.963 回答