So I am writing an algorithm that plays kind of a board game against my classmates algorithm. It basically foresees every possible outcome of the game and chooses the best path based on win% of taking each path. In the game each player has 5 seconds to make his move. Since it all runs on one PC I cannot use processing power or too much memory space during my opponents move.
I am using a Tree structure with each Node looking like below:
public class Node {
//coordinates on the board
private Move move;
//winpercentage for choosing this path
private double winPercentage;
//is this move mine to make or is it opponents (the last person to make a move wins the game)
private boolean odd;
//all possible next moves
private Set<Node> children;
//previous move
private Node parent;
}
I am using a wrapper class Tree which containts the root of the tree and some methods for traversing and building the structure.
public class Tree {
Node root;
public Tree() {
this.root = Node.createRoot();
}
//methods to traverse/build the tree
}
Even after I've built the entire tree and chose my move the tree remains stored in the memory during my opponents move which is forbidden since he cannot make his move while I've taken up 4GB of RAM. I've tried setting the reference to null to trigger GC but it doesn't seem to work
} else if(input.equals("tree")){
System.out.println("Showing tree structure");
Tree tree = new Tree();
//does all the work
tree.mainFunctionWrapper(tree.getRoot());
tree.setRoot(null);
tree = null;
}
Since its all in one code block I have no further nor previous references to the tree structure.
How do I make garbage collector work or maybe free the memory manually?