简体   繁体   中英

Memory leak when the same BufferedImage object is loading multiple images

I'm trying to make a tool that takes jpeg images (4 images) from a folder and merge them into one big image.

for (int i = desde; i < hasta + 1; i++) {
            for (int j = 0; j < 4; j++) {
                foto = ImageIO.read(new File("C:/picYou/Pic you_diciembre 06 2012/Pic you_take" + i + "/" + (j + 1) + ".jpg"));
                g.drawImage(foto, 300, 300 + j * (altoFoto + 20), null);
                foto.flush();
                foto = null;
            }
            File output = new File("C:/picYou/" + i + ".jpg");
            ImageIO.write(img, "jpg", output);
            g.finalize();
            g.dispose();
        }

g is a graphics2d object and foto is a BufferedImage.

The problem is that even though I flush the image and also make it null before loading the other image, the memory used by the images is not being freed. As you can see, every image is loaded in "foto", is there any way to make it more efficient? Thanks!

Memory is freed by the garbage collector when it is needed, not on demand.

If you want check if you have a memory leak I suggest you use VisualVM and look at how much memory is used only after a Full GC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM