简体   繁体   English

图像在处理中使用过多的 RAM

[英]Images use too much RAM in Processing

I'm trying to load a few thousand small images into processing and display them as a larger map.我正在尝试将几千张小图像加载到处理中并将它们显示为更大的 map。 The total filesize of the images together is 130MiB however when I run the program it uses all of my RAM and I even get an OutOfMemoryError as the RAM usage exceeds 2GiB.图像的总文件大小为 130MiB,但是当我运行程序时,它使用了我所有的 RAM,并且由于 RAM 使用量超过 2GiB,我什至得到了OutOfMemoryError

What causes over 10x the memory usage compared to the filesize, and is there any way I can mitigate this?与文件大小相比,是什么导致 memory 使用量超过 10 倍,有什么办法可以缓解这种情况?

EDIT:编辑:

Example code示例代码

ArrayList<PImage> images = new ArrayList<PImage>();
void setup() {
    for (int i = 0; i < 2000 /*num of images*/; i++) {
        images.add(loadImage(Integer.toString(i) + ".jpg");
    }
}

//in reality, never gets here
void draw() {

    for (PImage i: images) {
        image(i, /*precalculated x and y*/ random(500), random(500));
    }
}

JPEG compression ratio is 10:1, so that would explain it: the images are compressed on disk, but when loaded into your program and decompressed you get 10 times the size. JPEG 压缩比是 10:1,这可以解释:图像在磁盘上被压缩,但是当加载到您的程序并解压缩时,您会得到 10 倍的大小。

To improve your code, don't load the images all at once: process them few at a time.为了改进您的代码,不要一次加载所有图像:一次处理几个图像。

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

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