0

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!

vdrg
  • 369
  • 1
  • 3
  • 9
  • possible duplicate of http://stackoverflow.com/questions/4630081/found-what-kind-of-objects-are-causing-a-memory-leak-now-what – Robert H Jan 16 '13 at 15:45

1 Answers1

2

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I noticed that heap size and used heap stay the same after the application stops loading the images.. and for some reason "live bytes" allocated to byte[] continued incrementing.. – vdrg Jan 16 '13 at 16:56
  • You only know what is "live" after a GC. Until that happens you can only see what has been used, even if it is not in use now. – Peter Lawrey Jan 16 '13 at 17:24