[EDIT]Problem solved, I found the leak. Thank you very much.
I’m running a scheduling algorithm, with nodes that stores the following data:
int job[] = new int[Algo.NUM_MACHINES];
int remTime[] = new int[Algo.NUM_MACHINES];
int res = Algo.RESOURCE;
int time = 0;
int tardy = 0;
//Job jobTree[] = new Job[Algo.NUM_JOBS];
ArrayList<Integer> jobsFinished = new ArrayList<Integer>();
int id;
boolean visited = false;
Machine parent = this;//so that root node points to himself
ArrayList<Machine> children = new ArrayList<Machine>();
int duplicate = 0;//duplicate (sj-sj-sj-sj-...) flag
Each node has an ArrayList that leads to its children, and a parent node that leads to its parent.
When I determine a node is no longer needed, is
node.parent.children.remove(node);
node.parent=null;
enough to cause the node be recycled? How would you write the code to recycle the node?
To make an object recyclable in a garbage-collected environment, you have to ensure that there are no references to it from reachable objects.
Garbage collection sees what objects are reachable by following references. When you overwrite a reference or set it to
null, you’re burning a bridge to the target object, and if there’s no other way to get to that object, the memory can be reclaimed.As far as I can tell, you don’t need this line:
because
nodebecomes unreachable when it’s removed from the ArrayList (unless there’s a pointer to it elsewhere in the application). Ifnodeis unreachable, it doesn’t matter what it points to because it’s already sunk.If nodes aren’t being collected, but you think they should be, try to find out what is pointing to them. It might even be a parameter or variable up in the stack somewhere keeping the memory pinned.
Disclaimer: I don’t know Java, so I don’t know exactly how its VM works. I’m going by my knowledge of garbage collection in general.