gpt4 book ai didi

java - 使用Javax读取图像占用大量内存

转载 作者:行者123 更新时间:2023-11-29 06:13:18 24 4
gpt4 key购买 nike

我正在使用 JFileChooser 打开一堆文件,并使用 image = ImageIO.read(path); 为每个图像创建 BufferedImage。其中图像被声明为静态字段。

现在我有 30 个文件,每个文件 1Mb,在运行 60 次 read() 后,我的内存使用量(在操作系统程序管理器中检查)增长了大约 70Mb。

因为我的图像变量是静态的,所以图像内容存储在某处不是问题。所以我的问题是,为什么我会失去这么多内存?

我正在编写需要将大量图片加载到内存中的应用程序,是否有韭菜?清理未使用的数据是垃圾收集器的任务吗?

这是我读取这些数据的代码:

public class FileListElement {

private static final long serialVersionUID = -274112974687308718L;

private static final int IMAGE_WIDTH = 1280;

// private BufferedImage thumb;
private static BufferedImage image;
private String name;

public FileListElement(File f) throws IllegalImageException {
// BufferedImage image = null;
try {
image = ImageIO.read(f);
// if (image.getWidth() != 1280 || image.getHeight() != 720) {
// throw new IllegalImageException();
// }
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
//
// thumb = scale(image, 128, 72);
// image = null;

name = "aa";
}
}

这是怎么回事?

也许我做错了什么?我需要从加载到 RAM 的大量图像或压缩图像中获取原始像素。这样我就可以快速访问图像的任何像素。

奇怪的是,加载 1Mb 的图片占用的空间远远超过 1Mb。

最佳答案

您不能指望当前内存使用量是垃圾收集不会持续运行所需的内存量,尤其是当您远离最大内存使用量时。尝试加载更多图片,您可能会发现没有问题。

It's odd that loading 1Mb pic takes much more than 1Mb.

好吧,我希望存储在磁盘上的格式可能比内存中的 BufferedImage 压缩/更小。所以我认为这并不奇怪。

关于java - 使用Javax读取图像占用大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5996850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com