Is this a memory leak or am I just paranoid?

Hi everybody,

out of curiosity I monitored the memory usage of my game engine and noticed that it’s memory usage rises at a rate of about 4k/second (task manager, Windows 7 x64). So far the program only consists of an entity/component system and a simple game class for testing. Essentially my game loop looks like this:

void gameLoop() {
	if (scene == null) throw new RuntimeException("Critical: Game.scene must not be null.");
	millisPassed = System.currentTimeMillis();
	while (!gameEnd) {
		fdelta = (System.currentTimeMillis() - millisPassed) / 1000f;
		scene.update(fdelta);
		scene.render(fdelta);
		millisPassed = System.currentTimeMillis();
		calculateFps();
		yield();
	}
}

I already found out that when commenting the scene.update and scene.render method calls out the memory usage is constant, but the only thing I do in those methods is iterating over an empty ArrayList:


public class Scene implements IScene {

	protected ArrayList<Entity>	entities;

	public Scene() {
		entities = new ArrayList<Entity>();
	}

[...]

  	@Override
	public void update(final float fdelta) {
		for (Entity entity : entities) // <--- isEmpty
			entity.update(fdelta);
	}

	@Override
	public void render(final float fdelta) {
		for (Entity entity : entities)
			entity.render(fdelta);
	}
}

In their update and render method entities then call the update/render methods of their respective components which do the actual updating/rendering. As already mentioned the program currently iterates over an empty list of entities, so I don’t understand why the memory usage increases constantly.
It’s not a dramatic increase, from initially 6,000 KB it goes up to 15,000 in about half an hour, but it still concerns me as I assume that it’ll be much harder to find the reason for this when my engine is actually doing something.

Here are the Entity’s update and render methods:

	public void render(final float fdelta) {
		for (IRenderCallback c : renderers)
			c.render(fdelta);
	}

	public void update(final float fdelta) {
		for (IUpdateCallback c : updates)
			c.update(fdelta);
	}

I’m new to java and have no clue if this behaviour is normal or what causes it, so I hope you guys can explain it to me.
Thanks in advance!

This is normal. The for:each loop expands to something like :

So you see you have at least a new reference to a Iterator each iteration (even if the list is empty, you have to check the i.hasNext() .
So in half an hour all these references will sum up to something . The garbage collector will not run to clean up just a few KB, it still has a lot of memory to spend before wanting to run.

You will have a program that comes with java called jvisualvm.exe it is extremely useful for figuring out whats going on.

[quote=“teletubo,post:2,topic:36779”]
Good to know, actually pretty obvious. Thanks for pointing that out.

I’ll check it out, thanks.

There is also JConsole. Mine is located in the jdk/bin or jdk/lib depending upon whether one wants to use a .jar or a .exe.

There is this article about how to use it.
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

I’d like to hear about the relative merits of the different profilers, if others are willing. I’ve never used jvisualvm.exe, didn’t even know about it until reading the above.

I had good luck discovering some “leaking” threads (continually being created but never dying) during a session with jconsole. It is fascinating watching the GC cycles, as data is moved from “Eden” to “Survivor” to …

[EDIT: JVisualVM.exe is really great! I’ve started using it. Found this article that might help others also new to this tool: http://java.dzone.com/news/visual-vm-free-and-open-source. The utility itself has links to “official” articles which are very helpful, too.]

Really off topic but quite curious naming you have there.
What is with the “I”? :slight_smile:

[quote]Really off topic but quite curious naming you have there.
What is with the “I”?
[/quote]
My guess is “I” is for interface.

Yes, that denotes an interface. I tried to use adjectives for interface names but to me it started to look silly pretty soon (for each Updateable do update…). So I ended up using substantives. My problem is that they tend to clash with class names and thats why prefixing interfaces with “I” works for me.

I thought this was pretty common:

Oh I’ve never seen that before :stuck_out_tongue:

It seems to be the standard in the Eclipse platform.