Memory error when trying to load many image files

I ask for help and forgiveness if the question is stupid. But I ran into a problem.
I am from Ukraine and my English is bad, so I am writing through a translator, do not hit for errors, please)
The essence of the problem:
I wrote the program. Was at the stage of loading the necessary files and saw that a memory error occurs.
I began to understand the problem and this is what I came up with:
IF ATTACHING MORE THAN 204 FILES THAT IS THE CLOSING OF A SKETCH AND A ERROR:
a.lang.OutOfMemoryError: Failed to allocate a 1280012 byte allocation with 648856 free bytes and 633KB until OOM

How did I come to the number 204? I created a stress test. I attached pictures many times and fixed the number and total weight of the added files.
Initially I thought that it was a matter of weight, but practice showed that the weight could be 130 megabytes and 76 and 35 megabytes. I checked it on three files of different weights. So it’s not about weight?
HERE IS THE STRESS TEST CODE:

PImage[]twis=new PImage [1000];
PImage[]test=new PImage [1000];
int stage=0;
float mb=0;

void setup(){
fullScreen();
background(200,200,200);}

void draw(){
background(10,10,10);
if(mousePressed==true){
stage++;
mb+=0.465;
twis[stage]=loadImage(“a1.png”);
mb+=0.645;
test[stage]=loadImage(“a12.png”);
mousePressed=false;}
textSize(30);
fill(200);
text("Прогрузилось "+stage, 100,100);
text("ВЕС mb "+mb, 100,150);}

And yes, the problem initially arose when I attached the files I needed to the main sketch. They were drawn in the program as the files for the test.
But Processing did not want to attach more than 30 pieces. I do not know what the matter is, because I replaced these pictures with something else and everything worked again up to 204 files. Maybe this is some kind of anomaly, I do not know))

But now the main question is 204 files.
Is this a limitation or what? I need more files)) And yes, I write on android.

Thank you.

1 Like

Hey There!

Looping through so many images and loading them over and over will be taking alot of memory loadingImages should be done in setup.

I tried to do this in setup () {}; but the error from 204 is not lost.

1 Like

Set your memory in processing higher in file - preferences. Also put s for loop which scrolls through each array index and loads the images.

I apologize, I do not know how to put the memory above. Can this be done with android?

for () tried, but it did not help.

tried and manually in setup:
imageName[stage]=loadImage(…
stage++;
imageName[stage]=loadImage(…
stage++;
imageName[stage]=loadImage(…
stage++;
=Error 204. ;(

Ah sorry I misread . The sketch is being loaded on a phone then ? Are you using a real android phone or emulating ?

nothing wrong that confused. I mentioned this at the end and did not focus attention. my mistake.

Really android.

The device your writing code is Android then ?

Yes. Yes. Android Game)

I think we need more context and more code.

I sent all the stress test code, it is all from A to Z. It has an error. It’s not about the code, though, I think that there might be a phone case? or you can somehow remove the “restriction” … can somehow allocate more memory? but the matter is not in it, namely in 204 files. If you have the opportunity, then check on your device, please. APDE weighs a little, the code above. Can you?

I found a solution to the problem. You need to set the previous image to zero so as not to clog the memory))

1 Like

Hello. I am trying to load lots of images as well. Can you please share your code?

Thanks

Hi.

Try it:
image[22]=null;

One concept to explore is to load only the images that you need as conceptually explained here. Just to clarify: A stress test is done to test your application against normal or peak usage. Your test was steered toward finding upper limits which depend on allocated resources. If you increase the resources available to your application, you can load more images but you always need answer the question: do you really need all these images in memory?

Kf

1 Like