【问题标题】:Exception in thread "Thread" java.lang.OutOfMemoryError: Requested array size exceeds VM limit线程“线程”java.lang.OutOfMemoryError 中的异常:请求的数组大小超过 VM 限制
【发布时间】:2014-02-07 16:56:06
【问题描述】:

唯一的区别是有两个不同的作物位置。 问题是为什么我会收到这个错误??

方法调用

CropRealOriginalImage1 orderName = new CropRealOriginalImage1();
        FourAreaCropAgain1 orderNameFirst=new FourAreaCropAgain1();
        orderNameFirst.orderNameFirst();
        Decode decode= new Decode();
        decode.inputImage("C:/TEMP/Image/Embed Image/Four Area/OrderFirst.png");
        if(decode.s.equals("")){
            System.out.println("OderFirst=null");
        }else{
            //put b into txt file
            System.out.println("decode.s" +decode.s);
        }

工作:

  public void orderNameFirst(){
       ImageIcon icon = new ImageIcon("C:/TEMP/Image/Embed Image/Really Original.png");
    image = icon.getImage();
    image = createImage(new FilteredImageSource(image.getSource(),
        new CropImageFilter(icon.getIconWidth()-290, 0, 10, 33)));
            //new CropImageFilter(icon.getIconWidth()/2, icon.getIconHeight()/2, icon.getIconWidth()/2, icon.getIconHeight()/2)));

    BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),
            icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics graphics = bufferedImage.getGraphics();
    graphics.drawImage(icon.getImage(), 0, 0, null);

    Graphics2D g = bufferedImage.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(icon.getIconWidth()-290, 0, 10, 33);

 }

不工作

   public void orderNameFirst(){
        ImageIcon icon = new ImageIcon("C:/TEMP/Image/Embed Image/Really Original.png");
    image = icon.getImage();
    image = createImage(new FilteredImageSource(image.getSource(),
        new CropImageFilter(3*icon.getIconWidth()/8, 0, icon.getIconWidth()/8, icon.getIconHeight()/2)));
            //new CropImageFilter(icon.getIconWidth()/2, icon.getIconHeight()/2, icon.getIconWidth()/2, icon.getIconHeight()/2)));

    BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),
            icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics graphics = bufferedImage.getGraphics();
    graphics.drawImage(icon.getImage(), 0, 0, null);

    Graphics2D g = bufferedImage.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(3*icon.getIconWidth()/8, 0, icon.getIconWidth()/8, icon.getIconHeight()/2);
   }

错误: 解码整数长度:2147483647 线程“线程”java.lang.OutOfMemoryError 中的异常:请求的数组大小超出 VM 限制

【问题讨论】:

  • 你忘了问问题
  • 请求的数组大小超过了 VM 限制 -- 对我来说似乎很清楚。
  • (除了实际提出问题外,您还应该将异常堆栈跟踪复制到您的问题中,并确定代码中与异常对应的行。)
  • 我会尝试更新问题。谢谢你告诉我。
  • @JarekHuang:除了堆栈跟踪,发布图像和图标尺寸。 :-)

标签: java bufferedimage graphics2d imageicon


【解决方案1】:

免责声明:这可能不是您想要的答案,但这正是您所要求的。

问题是为什么会出现这个错误??

你得到错误:

 Exception in thread "Thread" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

...因为您正在尝试创建一个大于 Java VM 堆中最大连续内存块的数组。这可能是因为您正在尝试创建一个非常大的映像,或者可能是您的虚拟机在您尝试分配阵列时通常资源不足。

这很可能发生在BufferedImage 构造函数之一中。

但很难说,因为您还没有发布完整的堆栈跟踪,也没有发布有关图像大小或其他在运行时在程序中实际传递的值的相关信息。

修复取决于内存不足的原因。

例如,我可以从您的代码中看到,您从未在您正在创建的 Graphics/Graphics2D 实例上调用 dispose。随着时间的推移,这可能会导致资源泄漏(只是一个示例,可能还有其他示例)。

如果你只是立即用完内存,因为图像很大,你需要增加最大堆大小。这通常通过将-Xmx<value> 参数传递给java 命令行来完成(其中<value> 是新的最大大小,例如256m1G 或类似的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 2018-10-13
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多