Bug 1101 : Memory leak with textFont() in JAVA2D
Last modified: 2009-05-26 11:05




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

 

Reporter:
bredend
Assigned To:
fry

Attachment Type Created Size Actions

Description:   Opened: 2008-12-13 17:52
The following codes reaches 500MB memory usage in less than a minute:

PGraphics temp;
PFont font;

void setup(){
size(100, 100, JAVA2D);
font = loadFont("ArialMT-26.vlw");
}

void draw(){
temp = createGraphics(100,100, JAVA2D);
temp.beginDraw();
temp.textFont(font);
temp.text("hello", 10,10);
temp.line(40, 40, mouseX, mouseY);
temp.endDraw();
image(temp,0,0);
}

When P2D or P3D is used in the createGraphics method it works well. Also
when the part about text is removed.
Maybe the JAVA2D renderer has problems disposing the graphics if text is
drawn because the memory use increases a lot when i try to draw images etc.

I used 1.0.1 and 0157 (with Java) on WinXP SP3.


java.lang.OutOfMemoryError: Java heap space
at processing.core.PGraphics.<init>(PGraphics.java:471)
at processing.core.PGraphicsJava2D.<init>(PGraphicsJava2D.java:91)
at sun.reflect.GeneratedConstructorAccessor1.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at processing.core.PApplet.makeGraphics(PApplet.java:1136)
at processing.core.PApplet.createGraphics(PApplet.java:1046)
at xmltest.draw(xmltest.java:26)
at processing.core.PApplet.handleDraw(PApplet.java:1399)
at processing.core.PApplet.run(PApplet.java:1304)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Animation Thread" java.lang.RuntimeException: Java
heap space
at processing.core.PApplet.makeGraphics(PApplet.java:1161)
at processing.core.PApplet.createGraphics(PApplet.java:1046)
at xmltest.draw(xmltest.java:26)
at processing.core.PApplet.handleDraw(PApplet.java:1399)
at processing.core.PApplet.run(PApplet.java:1304)
at java.lang.Thread.run(Unknown Source)
Additional Comment #1 From fry 2008-12-13 18:02
You're creating a new graphics context, converting that to a pixel array
and rending it at a rate of 60 times per second. That's ridiculous.

Don't call createGraphics() inside draw() unless you absolutely must, but
if you do, don't do it at 60 frames per second the way that this code does.
Additional Comment #2 From bredend 2008-12-14 06:55
The code was just an example. It's ridiculous indeed. But with more drawing
to the graphics (rendering an email) it will use 50MB per PGraphics.
Additional Comment #3 From accadm 2009-05-26 11:01
I've had a similar experience with JAVA2D. I'm trying to draw text around
the screen at different scales and orientations. When I use matrix
operations (translate, rotate, scale), I encounter a sizable memory leak.
This doesn't occur with the other renderers, but they have other drawbacks
so I can't use them. Here's a basic program which demonstrates the error:

PFont font;
int index = 0;

void setup () {
size (200, 200, JAVA2D);
font = createFont("Courier", 40);
textFont(font);
}

void draw () {
background(200);
pushMatrix();
translate(100, 100);
scale(1);
rotate((float)index/100.);
translate(-3, 3);
fill(100);
text((char)('A' + index%26), 0, 0);
popMatrix();
index++;
}

Any word on this leak would be appreciated. Thanks.
Additional Comment #4 From fry 2009-05-26 11:05
that's a different issue, please post a separate bug report, rather than
adding to an already closed bug.