vdrg
vdrg

Reputation: 369

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!

Upvotes: 0

Views: 902

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

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

Upvotes: 2

Related Questions