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

戏子无情 提交于 2019-12-13 05:58:55

问题


The only thing difference is there is two different crop location. The question is why i get this error??

Method call

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

Work:

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

 }

Not Work

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

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


回答1:


Disclaimer: This might not be the answer you want, but this is what you asked for.

The question is why i get this error??

You get the error:

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

...because you are trying to create an array that is larger than the maximum contiguous block of memory in your Java VMs heap. This can happen either because you are trying to create a humongously large image, or it could be that your VM is just generally running low on resources at the time you try to allocate the array.

This most likely happens inside one of the BufferedImage constructors.

But it's hard to say, as you haven't posted the full stack trace, nor the relevant information about the images' sizes or other values actually passed around in your program at runtime.

The fix depends on the reason for running out of memory.

As an example, I can see from your code that you never invoke dispose on the Graphics/Graphics2D instances you are creating. This will likely cause a resource leak over time (just an example, there might be others).

If you just run out of memory immediately, because the image is huge, you need to increase the maximum heap size. This is typically done, by passing a -Xmx<value> argument to the java command line (where <value> is the new max size, like 256m, 1G or similar).



来源:https://stackoverflow.com/questions/21633505/exception-in-thread-thread-java-lang-outofmemoryerror-requested-array-size-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!