I probably found a memory leak in TextRenderer class in JOGL 1.1.1-RC8.
It appears when many TextRender are allocated. Even if the dispose method is called, all the ressources allocated by a TextRenderer aren’t freed.
I created a small test case:
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.geom.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;
/** Shows how to place 2D text in 3D using the TextRenderer. */
public class TestTextRenderer implements GLEventListener {
private GLU glu = new GLU();
public static void main(String[] args) {
Frame frame = new Frame("Text Cube");
GLJPanel canvas = new GLJPanel();
final TestTextRenderer demo = new TestTextRenderer();
canvas.addGLEventListener(demo);
frame.add(canvas);
frame.setSize(640, 480);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.show();
animator.start();
}
public void init(GLAutoDrawable drawable) {
// normally renderer should be created here
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
glu.gluLookAt(0.5, 0.5, 10,
0.5, 0.5, 0,
0, 1, 0);
gl.glColor3d(1.0,1.0,1.0);
// draw text
TextRenderer renderer = new TextRenderer(new Font("Dialog", Font.PLAIN, 12));
renderer.setColor(1.0f, 1.0f, 1.0f, 1.0f);
renderer.begin3DRendering();
renderer.draw3D("Hello JOGL!!!",
(float) Math.random(),(float) Math.random(), (float) Math.random(), 0.01f);
renderer.end3DRendering();
gl.glPopMatrix();
// should get memory back
renderer.flush();
renderer.dispose();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(15, (float) width / (float) height, 5, 15);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
}
Basically, it is just printing some text on the screen. I agree that it is useless and time consuming to create a new text renderer each time display is called. However, it might be needed to recreate a new textrenderer outside the init method, especially to change the font.
If you run the code above, you’ll see that the programm use more and more memory.
But maybe I’m using TextRenderer in an incorrect way.
If not I will report the bug on the bug tracker.
Thank you,
Jean-Baptiste