Loading...
Logo
Processing Forum
Why do i get an Out of Memory Error when i run add this line of code:

Copy code
  1. for(int i=0;i<40;i++)
    {
    image(imgBlocks.get(0,0,16,16),i*16,352);
    }
After 30 seconds of normal working it lag up and gives me an out of memory error. Did i miss something? XD
I'm pretty sure the answer is obvious.
I guess the .get command loads the picture every time so it will lag up quickly...
So how can i avoid that?

Replies(3)

is that what you are trying to achieve ? (kind of…)
Copy code
  1. PImage img[] = new PImage[40];
    void setup()
    {
      size(500, 500);
      for (int i=0; i<40; i++)
      {
        img[i] = loadImage(i+".jpg");
      }
      for (int l =0; l<40; l++) {
        image(img[l], l*16, 0, width/16, height);
      }
    }


Search OutOfMemoryError and image in the forum, you will surely have lot of threads about this topic...
(That's a bug in the latest versions of Processing. It has a workaround.)
Here's some link for a post w/ a fix to this nasty PGraphics cache memory leak:

Just an extra observation, but it has nothing to do w/ that Processing beta bug:
Method get() from class PImage ->  http://processing.org/reference/PImage.html
generates a new memory block allocation every time it's invoked!

Since you aren't keeping their references inside some variable, 
those blocks become orphans just after creation.

However, Java's Garbage Collection will take care of it!  
As long as you don't breed them faster than GC can deal w/ them, you'll never run outta memory!  

But either way, since you're copying the exact portion of your PImage/ PGraphics over & over... 
Why not temporarily store it in a local variable once and use it inside your loop block?
This way, you gain performance and spare the GC a lot!!!

PImage img = imgBlocks.get(0, 0, 16, 16);

for ( int i = 0; i != 40; image(img, 16*i++, 352) );