1

我正在使用 JFileChooser 打开一堆文件,并且对于我BufferedImage使用image = ImageIO.read(path);. 其中 image 被声明为静态字段。

现在我有 30 个 1Mb 的文件,在运行 60 次 read() 后,我的内存使用量(在 OS 程序管理器中检查)增长了大约 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 还要多。

4

1 回答 1

3

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

奇怪的是,加载 1Mb 的图片比 1Mb 还要多。

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

于 2011-05-13T19:39:19.077 回答