2014-02-07 41 views
0

唯一的区别是存在两个不同的作物位置。 问题是为什么我得到这个错误?线程“Thread”中的异常java.lang.OutOfMemoryError:请求的数组大小超过VM限制

方法调用

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); 
    } 

错误:解码integerLength:2147483647 异常的线程“主题” java.lang.OutOfMemoryError:要求数组大小超过VM限制

+0

你忘了问一个问题 – Durandal

+0

请求的数组大小超过VM限制 - 看起来很明显。 –

+0

(除了实际提出问题之外,还应该将异常堆栈跟踪复制到您的问题中,并确定代码中与异常相对应的行。) –

回答

2

免责声明:这可能不是你想要的答案,但是这是你问什么。

The question is why i get this error??

你得到的错误:

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

...因为你要创建一个数组,它是不是在你的Java虚拟机的堆内存的最大连续块大。发生这种情况的原因可能是因为您试图创建大型图像,或者您尝试分配阵列时虚拟机通常资源不足。

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

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

该修复取决于内存用完的原因。

举例来说,我可以从您的代码中看到,您从不会在您创建的Graphics/Graphics2D实例上调用dispose。这可能会导致资源泄漏(只是一个例子,可能有其他的)。

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

相关问题