BufferedImages数组导致高内存使用率

时间:2019-04-20 21:40:54

标签: image memory

我有一个BufferedImages数组,它占用的RAM比我期望的更多,或者也很喜欢。这是代码:

public BufferedImage[] bufferVideo(String videoName, int framesToBuffer) {
    BufferedImage[] frames = new BufferedImage[framesToBuffer];
    for (int i = 0; i < framesToBuffer; i++) {
        String path = videoName + i + ".jpg";
        try {
            frames[i] = ImageIO.read(getClass().getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return frames;
}

每个帧在磁盘上大约175 KB。当我调用bufferVideo(LionKing,500)时,我希望RAM使用量会增加87.5 MB(或175 KB * 500)。但是,在观看TaskManager时,我的程序开始使用大约1700 MB以上的RAM。

1 个答案:

答案 0 :(得分:0)

磁盘上的图像采用压缩的JPG格式,但是BufferedImage中的图像已解压缩为其直接可用的格式(通常为rgb24)。 1:20的因数(大约是您所经历的)似乎很合适。

您的选择包括不预先读取所有500个文件,而仅保留解压缩图像的较小缓存并按生产者-消费者模式按需读取其他文件。

相关问题