Linked Lists and memory management.

In my game, I use a linked list (the concept, not the built in Java one), to add/remove entities. I fear however, that when an entity is removed from the linked list and it’s reference is set to null, that it is not completely being destroyed. I believe that Java sometimes keeps ties to my ‘deleted’ nodes long after I have tried to get rid of them.

What I know: Java sometimes gets rid of my nodes… eventually. I know that it sometimes takes the garbage collector a while to unallocate memory. I know that at least some nodes are eventually removed- otherwise the game would crash from memory overflow. (Since I’d keep making new nodes without removing old ones.)

What I don’t know: In my game, I sometimes get erratic behavior when nodes are deleted which leads me to believe that sometimes they arn’t deleted at all. I have no way of testing this however, which is why I’m asking here if anyone has seen/done any testing on this.

Here’s a simple example of how I’m doing this:

http://www.funkapotamus.org/linkedlist/Node.java
http://www.funkapotamus.org/linkedlist/Test.java

Oh, and if anyone knows a better method for dynamically spawning and killing entities (particles, monsters, bullets, etc.) in a high fps game, I’m open to suggestions.

What are the symptoms that lead you to beleive your entities arent being destroyed?

Looking at your test code the link list should set entities free just fine. Do your entities keep hard references to each other at any time? In which case it may be those references from a live entity to a dead one that stop it from being garbage collected.

I just new() my entities and particles and let them get GC’ed now and everything is fine. Once in a blue moon it skips a frame or two when they get GC’ed but not so as you’d notice.

Cas :slight_smile:

in removeStartNode…

if(node != null)
node.setPreviousNode(null);

headNode.setNextNode(null)<< this is missing

headNode = node;

same type of mistake in removeEndNode. you should probably add a method to class node that performs this action.

node::delete()
next = null
prev = null