Garbage Collector

Okay, so I made a dungeon generator, and it works perfect. The only downside, is that it consumes all of my memory after generating new dungeons.

I’ve deducted that it may be in the list, rooms, because the generator generates Room objects, and stores it in the array list. Then, the tilemap is made by adding all of the widths and heights of the rooms IN that list. Thing is, I can’t clear the list until I’m done with the dungeon, because references are made to each individual room for effects in the game.

Anyway, I get the heap space error after generating about 4 dungeons, and watching task manager, I can see Garbage Collector doesn’t seem to be getting rid of the memory used by the last dungeon, and just accumulates the used memory until the game breaks.

I’ve taken all of the precautions to try and make sure the game doesn’t run out of memory, but alas it hasn’t worked.

This includes reinitializing all of the variables, clearing the room list, and mapTiles array before generating the new map.

What should I do? :frowning:

Try to use profiler for more detailed of root of ebil.

Get rid everything that you used on previous dungeon by set to null and always remember to clear List or array after use. And the referenced objects too.

How do I “set to null”? I thought clearing the list got rid of all the objects.

EDIT:
I solved the problem by just making a simple workaround. I didn’t want to resort to it for a few reasons…but I completely reinitialize the entire object, which fixes the problem. If anyone can still help me overcome this, I’d be thankful.

Either you still keep references of objects you don’t need anymore or you simply did not assign enough memory to your game.
The various available garbage collectors have actually been quite perfected over the years, I think.

Setting to null is nothing more than:
objectX = null;

But you need to remove all references to be found.

I never set anything to null, and think that this a very bad approach in java.
The Garbage Collector is there that you don’t need to clear resources like in C, what setting something to null is doing.

I think your problem is that you are using global variables, which should be avoided.
Normally your Object should just get out of scope and get collected after that.

i.e.
You have a dungeon object, which is remembered in a reference in your game object.
When you enter a new dungeon, this reference just points to the new dungeon and the old will get automatically collected.
If you really need all dungeons every created to be stored in memory, you have to think about a new more memory efficient way to store them.

No, usually you don’t have to. Exceptions are for example array based collection classes like ArrayList or HashMap which have to null removed object references in order to trigger the GC.

If a single scope is so confused and overly large as to require explicitly nulling anything, you need to fix your design so you don’t. You should never have references in scope that are deliberately “broken”.

I’ve had good luck with the Eclipse Memory Analyzer (http://www.eclipse.org/mat/), which does offline profiling of heap dumps. There’s a standalone version of it so even if eclipse isn’t your preferred IDE you can still use it easily.