# Memory error when trying to load many image files

**URL:** <https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634>\
**Category:** Processing for Android\
**Created:** [July 9, 2019, 11:35pm UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634 "2019-07-09T23:35:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 9, 2019, 11:35pm UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/1 "2019-07-09T23:35:04Z")

</div>

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);}

 ![Screenshot_20190710-021157](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d58bf548a94459b77c2f61fc7b583e636b3d2ffc.png)

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.

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 10, 2019, 1:39am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/2 "2019-07-10T01:39:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 10, 2019, 1:42am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/3 "2019-07-10T01:42:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 10, 2019, 1:44am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/4 "2019-07-10T01:44:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 10, 2019, 1:49am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/5 "2019-07-10T01:49:55Z")

</div>

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. ;(

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 10, 2019, 1:52am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/6 "2019-07-10T01:52:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 10, 2019, 1:54am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/7 "2019-07-10T01:54:59Z")

</div>

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

Really android.

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 10, 2019, 1:56am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/8 "2019-07-10T01:56:02Z")

</div>

The device your writing code is Android then ?

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 10, 2019, 1:59am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/9 "2019-07-10T01:59:19Z")

</div>

Yes. Yes. Android Game)

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 10, 2019, 1:59am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/10 "2019-07-10T01:59:57Z")

</div>

I think we need more context and more code.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 10, 2019, 2:04am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/11 "2019-07-10T02:04:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 11, 2019, 2:39pm UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/13 "2019-07-11T14:39:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hbtousa](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@hbtousa](https://discourse.processing.org/u/hbtousa)\
**Post date:** [August 19, 2019, 6:47pm UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/14 "2019-08-19T18:47:51Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [August 20, 2019, 3:36pm UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/15 "2019-08-20T15:36:26Z")

</div>

Hi.

Try it:  
image[22]=null;

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 31, 2019, 4:37am UTC](https://discourse.processing.org/t/memory-error-when-trying-to-load-many-image-files/12634/16 "2019-08-31T04:37:29Z")

</div>

One concept to explore is to load only the images that you need as conceptually explained [here](https://forum.processing.org/two/discussion/23381/memory-problem#latest). 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
