繁体   English   中英

当同一BufferedImage对象加载多个图像时发生内存泄漏

[英]Memory leak when the same BufferedImage object is loading multiple images

我正在尝试制作一种从文件夹中获取jpeg图像(4张图像)并将其合并为一个大图像的工具。

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是graphics2d对象,而foto是BufferedImage。

问题是,即使我在加载另一个图像之前刷新了图像并将其设置为null,也不会释放图像使用的内存。 如您所见,每个图像都以“ foto”加载,有什么方法可以使其更高效? 谢谢!

垃圾收集器在需要内存而不是按需内存时将其释放。

如果要检查是否有内存泄漏,建议您使用VisualVM并查看仅在Full GC之后才使用多少内存

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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