[HELP] Memory Leak (Simple Game)

I am getting a huge memory leak in my very simple game.

I have tried profiling the VM, and apparently there are many, many integer arrays (int[])

Here is the source, please take a look, it is a simple game: http://dl.dropbox.com/u/26438996/Block%20Evader.zip

…Oh my God. :0

Where are these int arrays?

I have no idea. :’(

Debugging and stepping through your code can be helpful and good to get used to.

If i was you, i would take a look at this section:


while (true) {
				Enemy enemy = world.getEnemies().get(Misc.random(world.getEnemies().size() - 1));
				Direction dir = Direction.values()[Misc.random(Direction.values().length-1)];
				for (int i = 0; i < 15; i++) {
					enemy.move(dir);
					Thread.sleep(10);
				}
				Thread.sleep(10);

			}

I’m retarded, don’t worry. :confused:

I do not think so. If I comment that out, I get the same results.

I figured out what’s hogging the memory: DataBufferInt

Here is some info I found: http://www.symphonious.net/2007/04/26/japplet-memory-leaks/

I think there’s no need to create image on every paint.
Create it once and then fillrect with background color if any.


@Override
public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    paintWorld(dbImage.getGraphics());
    g.drawImage(dbImage, 0, 0, this);
    repaint();
}

Yup your problem is the paint method in your Applet class. You are creating a new Image every frame! Create it once and then reuse it.

Also, you shouldn’t be calling repaint() every frame, instead you should call it in your game loop.