java.lang.OutOfMemoryError: Java heap space when stitching 13k .png images together

谁说我不能喝 提交于 2019-12-04 14:28:19

You don't need to keep all your chunk images in memory. You can read them one by one and draw to your final image in final loop like this:

for (int i = 0; i < rows; i++) {
    for (int k = 0; k < cols; k++) {
        BufferedImage buffImage = ImageIO.read(imgFiles[num]);
        finalImg.createGraphics().drawImage(buffImage, null,
                chunkWidth * k, chunkHeight * i);
        num++;
    }
}

This will save at least half of the memory.

You need to increase the maximum memory for your program, not the maximum of eclipse which doesn't need more memory I assume.

There is an option in eclipse for changing the VM arguments (I don't know that it is as I haven't used it for many years)

All the same you need to be able to load all the images uncompressed. This means you need at least 13225 * 240 * 240 * 4 bytes. This is just over 3 GB. If you load all the images first, you need at least double this. I suggest you make the heap at least 4 - 8 GB.

Look like everyone is offering a simpler and less resource consuming solution. Let me try mine:

public static void merge_images() throws IOException {
    int rows = 115; 
    int cols = 115;

    System.out.println(rows * cols);

    // Initializing the final image
    BufferedImage finalImg = null;
    for (int i = 0; i < rows; i++) {
        for (int k = 0; k < cols; k++) {
            BufferedImage bufferedImage = ImageIO.read(new File("G:\\Images\\Image " + i + "-" + k + ".png"));
            int chunkWidth = bufferedImage.getWidth();
            int chunkHeight = bufferedImage.getHeight();
            if (finalImg == null) {
                finalImg = new BufferedImage(chunkWidth * cols, chunkHeight * rows, bufferedImage.getType());
            }
            finalImg.createGraphics().drawImage(bufferedImage, null, chunkWidth * k, chunkHeight * i);
        }
    }

    System.out.println("Image concatenated.....");
    ImageIO.write(finalImg, "png", new File("fusions.png"));
    System.out.println("Image Saved, Exiting");
}

Try to run JVM with -Xmx1024m or the same with greater values. This value is the Heap size.

java -Xmx1024m -jar app.jar

Also, you can increase this value in your IDE.

As was pointed out by hoaz (Many thanks) I was taking too many steps than were necessary. My solution is as follows:

public static void merge_images() throws IOException {
        int rows = 115; // we assume the no. of rows and cols are known and each
                        // chunk has equal width and height
        int cols = 115;
        int chunks = rows * cols;

        System.out.println(chunks);

        // fetching image files
        File[] imgFiles = new File[chunks];

        int count = 0;
        for (int j = 1; j <= 115; j++) {
            for (int k = 1; k <= 115; k++) {
                imgFiles[count] = new File("G:\\Images\\Image " + j
                        + "-" + k + ".png");
                count++;
            }
        }

        // Initializing the final image
        BufferedImage finalImg = new BufferedImage(240 * cols,
                240 * rows, 13);

        int num = 0;
        for (int i = 0; i < rows; i++) {
            for (int k = 0; k < cols; k++) {
                BufferedImage buffImage = ImageIO.read(imgFiles[num]);
                finalImg.createGraphics().drawImage(buffImage, null,
                        240 * k, 240 * i);
                num++;
            }
        }
        System.out.println("Image concatenated.....");
        ImageIO.write(finalImg, "png", new File("fusions.png"));
        System.out.println("Image Saved, Exiting");
    }

Essentially what I have done is remove the BufferedImage[] and instead created a new BufferedImage during the nested for loop when I am building up the final image. Rather than using variables I have hard coded the bounds of the images and type of each image.

Thanks for pointing me in the right direction hoaz.

Regards

Jamie

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