Bug 150 : PGraphicsGL has memory leaking issues when creating a lot of successive PImage objects
Last modified: 2007-08-22 17:51




Status:
RESOLVED
Resolution:
FIXED -
Priority:
P2
Severity:
normal

 

Reporter:
fry
Assigned To:
fry

Attachment Type Created Size Actions
Memory leak example sketch application/zip 2007-03-07 17:15 247.70 KB

Description:   Opened: 2005-09-13 08:04
image/texturing code is not optimized and written in a kludgey way in an
attempt to get the beta out. need to go back and fix up the memory leaks
(textures currently re-allocated on each frame).
Additional Comment #1 From magnos 2005-09-13 08:49
import processing.opengl.*;
import processing.video.*;

Capture camera;
Silhouette sil;

void setup()
{
colorMode(RGB,255);
size(320 ,240, OPENGL);
camera = new Capture(this, width, height, 20);
sil = new Silhouette();
}

void captureEvent(Capture camera)
{
camera.read();
}

void draw()
{
sil.render();
}

class Silhouette
{
int[] pix;

PImage tmp;
Silhouette()
{
pix = new int[width*height];
}

void setValue(int i, int val)
{
pix[i] = val == 0 ? -100 : pix[i] + 15;
}

void render()
{
this.checkInput(camera.pixels);
tmp = new PImage(width,height);
int px;
for (int i=0; i<width*height; i++)
{
px = constrain(pix[i], 0,255);
tmp.pixels[i] = color(px, px, px) ;
}
image(tmp,0,0);
}

void checkInput(color [] video)
{
int threshold = 127;
float p_bri;

for (int i=0; i<video.length; i++)
{
p_bri = brightness (video[i]);

if (p_bri > threshold) sil.setValue(i,1);
else sil.setValue(i,0);// black
}
}
}
Additional Comment #2 From fry 2006-04-13 14:05
actually that code is also buggy.. you shouldn't create a new PImage for
tmp on each frame.. 1) create a class variable called tmp so that it gets
reused 2) create tmp once in the constructor and 3) call updatePixels()
after you change tmp:

class Silhouette
{
int[] pix;

PImage tmp;
Silhouette()
{
pix = new int[width*height];
tmp = new PImage(width,height);
}

void setValue(int i, int val)
{
pix[i] = val == 0 ? -100 : pix[i] + 15;
}

void render()
{
this.checkInput(camera.pixels);
int px;
for (int i=0; i<width*height; i++)
{
px = constrain(pix[i], 0,255);
tmp.pixels[i] = color(px, px, px) ;
}
tmp.updatePixels();
image(tmp,0,0);
}

void checkInput(color [] video)
{
int threshold = 127;
float p_bri;

for (int i=0; i<video.length; i++)
{
p_bri = brightness (video[i]);

if (p_bri > threshold) sil.setValue(i,1);
else sil.setValue(i,0);// black
}
}
}
Additional Comment #3 From fry 2006-04-13 18:12
k, so the other memory leaking issues are straightened out, i'm changing
the title for this bug and its priority since it's triggered by poorly
written code, but it's still a bug since it happens with OPENGL and not
P3D, which means that there are other issues lurking underneath.
Additional Comment #4 From nfgodinho 2006-12-09 09:22
Hi fry,

I developed a project in Processing but it crashes after 20 minutes because
of these memory leaks. I\'m trying to convert it to Vvvv but it\'s not being
easy. Do you have plans to have this issue fixed soon? Just to know what to
do: wait for Processing or move to Vvvv.

Thanks,
Nuno
Additional Comment #5 From fry 2006-12-10 21:13
did you read my last post on this thread? are creating a lot of PImage
objects inside draw()? if so, then you're just writing bad code. if you're
just getting out of memory errors while using opengl code, then you have a
separate problem that's been reported but i haven't been able to verify
because i don't have any code that reproduces the issue consistently.
Additional Comment #6 From nfgodinho 2006-12-19 16:06
Hi!

Well, both :)

1. Yes, I am loading images inside draw(). Unfortunately there is no way
around it since I need to load over one hundred different images which
makes it impossible to cache them. I already tried it and it runs out of
memory half way. What else can I do?

2. Yes, this only happens when I am using OPENGL. It runs smoothly in P3D.
If you want I can create a simple patch the reproduces this error.

Thanks!
Nuno
Additional Comment #7 From fry 2006-12-19 17:16
in that case, yes, please post a minimal version of the sketch that
triggers the problem so that i can try to track it down.
Additional Comment #8 From nfgodinho 2006-12-27 07:51
> in that case, yes, please post a minimal version of the sketch that
> triggers the problem so that i can try to track it down.

Hi Fry,

Here\'s a small sketch which in my computer runs out of memory after 5-6
minutes. Please understand that in my real case situation I\'m not loading
new images every frame, but every 20-30 seconds or so. In that case it runs
out of memory after 30-40 minutes. The image files I\'m using are 2 normal
140Kb 1200x800 jpeg images. You\'ll notice that the process memory will
always increase. Actually sometimes it frees some memory but anyway it
tends to get higher and higher. I have also tried to force GC but it didn\'t
solve the problem. If you switch to P3D mode it will run healthy forever.

import processing.opengl.*;

PImage bitmap;
int flag = 0;

void setup()
{
size(600, 400, OPENGL); //size of window and render mode
frameRate(1);
}
void draw()
{
if (flag==1)
bitmap = loadImage(\
Additional Comment #9 From nfgodinho 2006-12-27 07:53
[something went wrong and the code is incomplete. sorry. here it is again]

import processing.opengl.*;

PImage bitmap;
int flag = 0;

void setup()
{
size(600, 400, OPENGL); //size of window and render mode
frameRate(1);
}
void draw()
{
if (flag==1)
bitmap = loadImage("1.jpg");
else
bitmap = loadImage("2.jpg");
flag = 1-flag;
scale(0.5);
image(bitmap, 0, 0);
println(millis());
}
Additional Comment #10 From fry 2006-12-27 07:54
i need a version that i can actually run--please use the \
Additional Comment #11 From nfgodinho 2006-12-27 08:05
Hum... sorry, I didn't understand what you want me to use. Anyway I've just
sent you by email my patch in a RAR. I hope this is what you need.
Additional Comment #12 From nfgodinho 2007-03-01 08:08
Hi fry,

Any news on this bug?

Last time we talked here you asked me to send you a working patch to demo
the bug and I sent it to you but never got an answer. Did you get it?

I don't want to push it, I know you've been working a lot on all this. I
just want to know, since I have a project which depends on this.

Thanks!
Nuno
Additional Comment #13 From fry 2007-03-07 17:09
nope, no action. it's still a priority, of course.

i think i have your example, but if not, use tools -> archive sketch and
add it here as an attachment. unfortunately there was a problem with
bugzilla and it chopped off our other comments where i was try to explain
how to upload things.
Additional Comment #14 From nfgodinho 2007-03-07 17:15
edit]
Memory leak example sketch

Hi Fry,

Here's the example sketch. I hope it helps you understand the problem.

cya,
Nuno
Additional Comment #15 From fry 2007-06-11 12:52
found and fixed for 0125.
Additional Comment #16 From nfgodinho 2007-06-11 16:40
Excelent! Thank you so much for all your great work!
Additional Comment #17 From th7511 2007-08-22 15:57
hi,

I've quite the same problem than described here:
i do:
imgZ.loadPixels();
for(int j=0; j < nbslice-2; j++){
for(int i=0; i < 512 ;i++) {
imgZ.pixels[i+512*j] = color( b[indx-5*int(float(indx)/5)][382+i*512+j*512*512
+sliceZNb]);
}
}
imgZ.updatePixels();

and the imgZ is well displayed but each time i update it, the amount of memory increase.
in P3D, no problems, only with opengl. even with 0125 release.
if i remove loadpixels:i still have the problem, if'i remove updatePixels no more problem
(but of course no update).
I tried the pde attached here, no problem but if i increase the framerate (5), the problem
appear...
Cordialy,
Thierry
Additional Comment #18 From fry 2007-08-22 17:51
this is a separate issue, can you post a new bug for it and i can take a
look? and if possible, please try to create a very minimal sketch that
exhibits the problem and use archive sketch, and include it with the bug
report as an attachment.