I've put together something I call a "ViewManager" from a few OpenGL/Android tutorials. The problem is that I am having a memory leak. I get a GC notification every once in awhile and I don't think I am doing anything.
11-23 01:50:34.435: D/dalvikvm(954): GC_CONCURRENT freed 381K, 6% free 8024K/8519K, paused 18ms+6ms, total 64ms
My code starts by extending GLSurfaceView and implementing a CallBack and Runnable. I then have a gameLoop that gets created and started which handles updating the objects.
As you can see from my code, I don't have any objects in the list yet, so the leak is not occuring in the .draw
or the .update
methods of an object.
I ran the code with taking out the gameLoop and the memory leak appeared to stop. Does anyone have any hints at why I am receiving a memory leak? On a side note, when I remove the Thread.sleep(30)
call on the run method, I get the GC_CONCURRENT notification many times faster!
import java.util.LinkedList;
import java.util.ListIterator;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.view.MotionEvent;
public class ViewManager extends GLSurfaceView implements Runnable, GLSurfaceView.Renderer
{
private Camera camera;
private ListIterator<GameObject> objIterator;
private LinkedList<GameObject> objects;
private Thread gameThread;
private boolean running;
public ViewManager(Context context)
{
super(context);
setRenderer(this);
objects = new LinkedList<GameObject>();
camera = new Camera();
camera.setPosZ(-4.0f);
}
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT|GL10.GL_COLOR_BUFFER_BIT);
//3D Drawing
objIterator = objects.listIterator();
gl.glLoadIdentity();
camera.draw(gl);
while(objIterator.hasNext())
{
gl.glPushMatrix();
objIterator.next().draw(gl);
gl.glPopMatrix();
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
camera.getPreviouslyTouchedPoint().set((int)event.getX(),(int)event.getY());
}
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
camera.setPosX(camera.getPosX() - (camera.getPreviouslyTouchedPoint().x-event.getX())/100f);
camera.setPosY(camera.getPosY() - (camera.getPreviouslyTouchedPoint().y-event.getY())/100f);
}
camera.getPreviouslyTouchedPoint().set((int)event.getX(),(int)event.getY());
return true;
}
public void onSurfaceChanged(GL10 gl, int width, int height)
{
running = false;
gl.glViewport(0,0,width,height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float)width/(float)height,0.1f,100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
running = true;
gameThread = new Thread(this);
gameThread.start();
}
public void onSurfaceCreated(GL10 gl, EGLConfig arg1)
{
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
public void run()
{
ListIterator<GameObject> itr = objects.listIterator();
while(running)
{
itr = objects.listIterator();
while(itr.hasNext())
{
itr.next().update(0);
}
try
{
Thread.sleep(30);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}