We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Memory leak in OPENGL mode of Processing
Page Index Toggle Pages: 1
Memory leak in OPENGL mode of Processing (Read 1633 times)
Memory leak in OPENGL mode of Processing
Oct 19th, 2006, 3:24pm
 
Hi,

I have implemented a very simple slide show using multiple simultaneous images and alpha channel fade-in-fade-out.

Unfortunately, when I run this program memory keeps growing forever and after about 20 minutes it freezes with an out of memory error.

Some months ago I posted this problem here but still don't have a solution. Talking to another member I was able to confirm that he also had the same problem running a simplified version of my program (which I created for testing reasons) and he supports my idea that there is a memory leak in Processing.

When recently I got the latest Processing upgrade I was hoping that this problem had been solved but it is still there.

I believe it was fry who told me I couldn't use loadImage() inside draw(). But since I have over 200 pictures to display and they're quite big there is no other way around it that I know.

If I switch from OPENGL to P3D the problem doesn't happen. But I cannot use P3D since, unlike OPENGL, in this mode images get jagged when I rotate them. Also, P3D doesn't make use of hardware acceleration and this is needed when I want to smoothly change the alpha channel on 10 images simultaneously.

Below is the simple code that recreates the bug.
Help anyone?

Thanks!
Nuno

---------------------
import processing.opengl.*;

PImage bitmap;
int flag = 0;

void setup()
{
size(640, 480, OPENGL);                 //size of window and render mode
framerate(5);
}
void draw()
{
if (flag==1)
  bitmap = loadImage("a.bmp");
else
  bitmap = loadImage("b.bmp");
flag = 1-flag;
image(bitmap, 0, 0);
println(millis());
}
----------------------
Re: Memory leak in OPENGL mode of Processing
Reply #1 - Oct 19th, 2006, 6:34pm
 
Use two PImage objects called bitmapA and bitmapB, load the bitmaps into those objects in setup() and then use your same logic to display one or the other in draw().
Re: Memory leak in OPENGL mode of Processing
Reply #2 - Oct 20th, 2006, 1:09am
 
Thank you for you idea but unfortunately that doesn't solve my problem. I only posted this simple code to demo the bug. If you read the full post you'll see that I need to display several (10) images at the same time. I am currently using a dynamic array of PImage. And it is a kind of never ending slideshow so I cannot load images in setup() because I have over 200 images to display, 10 at a time.
Re: Memory leak in OPENGL mode of Processing
Reply #3 - Nov 6th, 2006, 11:04pm
 
Based on your code you're loading an image every frame, that will cause a huge memory problem.

You need to load the images you plan to display ahead of time, render them after they are loaded in your loop, and when you plan on switching images call a function to set all the images to equal null, then call the java garbage collector, but be prepared for a significant speed hit (don't do this each frame)

And you may want to say load one image every 30 frames or so till you have a new set of 10 loaded, then swap buffers of images and THEN unload the old ones and repeat.

To call the garbage collector in Java:

     Runtime r = Runtime.getRuntime();
     r.gc();
Re: Memory leak in OPENGL mode of Processing
Reply #4 - Nov 9th, 2006, 2:26am
 
Hi WebDext,

Thank you for your reply.

Don't worry because my real code is not loading images every frame. Actually it is loading a new image every 30 seconds or more.

In my real code I was already calling garbage collection and setting the image to null. But I was using System.gc() instead of using the Runtime object like you suggested. I don't know the difference.

I tried using your code but unfortunately the problem remains. RAM usages starts at 700Mb and goes up to 1,4Gb in about 20 minutes. Then I start getting "GL_ERROR at before bind: 0505  GL_OUT_OF_MEMORY" messages while it is still working and finally, after a couple minutes, I get a java exception:

java.lang.RuntimeException: java.lang.OutOfMemoryError
at processing.opengl.PGraphicsOpenGL.requestDisplay(PGraphicsOpenGL.java:172)
at processing.core.PApplet.run(PApplet.java:1409)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError etc etc

If you think you can help me I'd be happy to send you the code if you send me your email.

Thank you again,
Nuno
Re: Memory leak in OPENGL mode of Processing
Reply #5 - Nov 9th, 2006, 10:36am
 
I sent you a PM with my email address.

Jack
Page Index Toggle Pages: 1