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 & HelpPrograms › PGraphics outofmemory
Page Index Toggle Pages: 1
PGraphics outofmemory (Read 3191 times)
PGraphics outofmemory
Mar 30th, 2010, 10:52am
 
processing version 1.07
linux 64 bit
java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02, mixed mode)

The following code causes an outofmemoryexception. I know this probably appears to be a stupid thing to do, but this code is adapted from a larger project which (ab)uses the same principle. Looks like those PGraphics objects are not being garbage collected after being de-referenced - try instantiating just a single one in setup and it works.

Code:
PGraphics pg;

void setup(){
 size(500, 500, P2D);

}

void draw(){
 pg = createGraphics(8192, 8192, P3D);
 //pg.backgroud(255);
 pg.fill(0);
 pg.ellipse(0, 0, 25, 25);
 image(pg, 100, 100);
}
Re: I can't login to bugzille/ PGraphics outofmemory
Reply #1 - Mar 30th, 2010, 11:09am
 
sure because you create it again and again every frame. why not creating it only once in setup. should work.
and dont forget to use pg.beginDraw() and pg.endDraw();


thats how it should be

Code:
PGraphics pg;

void setup(){
size(500, 500, P2D);
pg = createGraphics(8192, 8192, P3D);
}

void draw(){
pg.beginDraw();
//pg.backgroud(255);
pg.fill(0);
pg.ellipse(0, 0, 25, 25);
pg.endDraw();
image(pg, 100, 100);
}
Re: I can't login to bugzille/ PGraphics outofmemory
Reply #2 - Mar 30th, 2010, 2:17pm
 
That's not a bug, you try to use more than 256MB, which is the default memory setting of Processing. Increase this value in the Preferences.
If you add println(frameCount); at the start of draw(), you will see only one frame is displayed, while if you reduce the size of the graphics, it runs without problem (after adding beginDraw()/endDraw(), of course).
Re: PGraphics outofmemory
Reply #3 - Mar 30th, 2010, 4:02pm
 
Thanks  Cedric and PhiLho for the quick answers!

How about this one? Yes I know that in this simple example, it is stupid to create a new PGraphics object every time draw is invoked, but like I said, it is a stripped down example that shows the problem. In the real project, I was creating new PGraphics objects much less often, but I still eventually had this crash:


Code:

import processing.opengl.*;

PGraphics pg;

void setup(){
size(500, 500, OPENGL);
}

void draw(){
 pg = createGraphics(7000, 7000, P3D);
pg.beginDraw();
pg.fill(0);
pg.ellipse(0, 0, 25, 25);
pg.endDraw();
image(pg, 100, 100);
}

cheers

Matthew
Re: PGraphics outofmemory
Reply #4 - Mar 30th, 2010, 4:28pm
 
but you dont need to recreate it, to redraw it, or do you  in your case ?
Re: PGraphics outofmemory
Reply #5 - Mar 31st, 2010, 1:05am
 
Garbage collection isn't instantaneous.
If you create several objects quickly, you might go out of memory before garbage is collected. Although I think Java will first run a GC cycle before throwing the error.
Note that in your last script, you have the memory of the PGraphics pg, but also the memory of the sketch, plus perhaps temp arrays, variables, and so on.

You can tweak the memory settings, the GC parameters (a complex topic in Java!), etc. to get optimal result.
Note that it might also depend on your version of Java (they regularly improve this field): this is more a Java issue than a Processing one (unless it has a memory leak, but I don't think so, Processing is quite conservative (in a good sense) in memory management).
Re: PGraphics outofmemory
Reply #6 - Mar 31st, 2010, 2:43am
 
got to post 5 times to show you what I mean in an image (3)
Re: PGraphics outofmemory
Reply #7 - Mar 31st, 2010, 2:43am
 
got to post 5 times to show you what I mean in an image (4)
Re: PGraphics outofmemory
Reply #8 - Mar 31st, 2010, 2:43am
 
got to post 5 times to show you what I mean in an image (5)
Re: PGraphics outofmemory
Reply #9 - Mar 31st, 2010, 2:43am
 
My conclusion is that it is a problem with the creation and garbage collection of offscreen image buffers that only occurs in OPENGL mode. It seems that the buffers are never garbage collected. In my real code, I was creating new and de-referencing old PGraphics objects as the program ran, which I thought would be fine but it seems to cause a memory leak. I have changed the code so only creates a single set of image buffers and reuses them. Here s the interface I am working on:

...

The (badly designed...) icons on the left of the rows are pre-rendered and drawn from PGraphics/ image, so I don't need to re-render them every time draw gets called. The number of rows can change, requiring scaling of the icons. I was creating new PGraphics objects at this stage at the new size and re-rendering the icons but this is where I had the memory leak problem (after a few resizing cycles). Now I just scale them when I call image, which looks a bit rubbish, but doesn't crash the program.

cheers

Matthew
Page Index Toggle Pages: 1