Hey there!
I have a code wich makes treenodes for the depth of 6 recursively. The problem is that i use too many reference, and i get an Out Of Memory exception. Exactly : "java.lang.OutOfMemoryError: Java heap space". I've tried to increaze the memory space with -J Xmx, but it doesn't seem to work. If I only create one tree, than it's ok, with two I run out of mem. Here's my code:
Main class:
Node class:Code:package faalgoritmus;
public class FaAlgoritmus {
public static void main(String[] args) {
// just making the first state, they are represented as a long number in the 24 scale (numeric system)
long firstState = 0;
int[] firstStates = {23,0,0,23,0,0,23,0,0};
for(int i = 0; i < 9; ++i){
firstState += firstStates[i] * Math.pow(24, i);
}
//making two node
Node root = new Node(null, firstState, 0);
System.out.println("elso kesz");
Node root2 = new Node(null, firstState, 0);
}
}
And the static tableCode:package faalgoritmus;
public class Node {
private Node[] children;
private Node parent;
private long states;
private int depth;
private static int[] statesToCalc = new int[9];
public Node(Node parent, long states, int depth) {
this.parent = parent;
this.states = states;
this.depth = depth;
this.children = new Node[12];
makeChildren();
}
//takes the states to an array
private void Calc(long states){
for (int i = 8; i > 0; --i){
statesToCalc[i] =
(int) (states / Math.pow(24, i));
states = (long) (states % Math.pow(24, i));
}
}
//calculates the new states
private static long makeNewStates(int fst, int snd, int trd, int dir){
long lolstates = 0;
for(int i = 0; i < 9; ++i){
if(i == fst){
lolstates += MovesTable.getTableElement(fst, dir) * Math.pow(24, fst);
}
if(i == snd){
lolstates += MovesTable.getTableElement(snd, dir) * Math.pow(24, snd);
}
if(i == trd){
lolstates += MovesTable.getTableElement(trd, dir) * Math.pow(24, trd);
}
else{
lolstates += statesToCalc[i] * Math.pow(24, i);
}
}
return lolstates;
}
//creates the children, depending on a static table
private void makeChildren(){
if(depth < 6){
Calc(states);
children[0] =
new Node(this, makeNewStates(0,3,6,0), depth + 1);
children[1] =
new Node(this, makeNewStates(0,3,6,1), depth + 1);
children[2] =
new Node(this, makeNewStates(1,4,7,0), depth + 1);
children[3] =
new Node(this, makeNewStates(1,4,7,1), depth + 1);
children[4] =
new Node(this, makeNewStates(2,5,8,0), depth + 1);
children[5] =
new Node(this, makeNewStates(2,5,8,1), depth + 1);
children[6] =
new Node(this, makeNewStates(0,1,2,2), depth + 1);
children[7] =
new Node(this, makeNewStates(0,1,2,3), depth + 1);
children[8] =
new Node(this, makeNewStates(3,4,5,2), depth + 1);
children[9] =
new Node(this, makeNewStates(3,4,5,3), depth + 1);
children[10] =
new Node(this, makeNewStates(6,7,8,2), depth + 1);
children[11] =
new Node(this, makeNewStates(7,7,8,3), depth + 1);
}
}
public Node getParent() {
return parent;
}
}
Code:package faalgoritmus;
public class MovesTable {
private static final int[][] table = {
{4, 8, 12, 16},
{17, 13, 5, 9},
{14, 18, 10, 6},
{11, 7, 19, 15},
{23, 0, 14, 17},
{19, 12, 22, 1},
{15, 16, 2, 21},
{3, 20, 18, 13},
{0, 23, 13, 18},
{16, 15, 1, 22},
{12, 19, 21, 2},
{20, 3, 17, 14},
{5, 10, 20, 0},
{1, 21, 7, 8},
{22, 2, 11, 4},
{9, 6, 3, 23},
{6, 9, 0, 20},
{21, 1, 4, 11},
{2, 22, 8, 7},
{10, 5, 23, 3},
{7, 11, 16, 12},
{13, 17, 6, 10},
{18, 14, 9, 5},
{8, 4, 15, 19}
};
public static int getTableElement(int row, int col) {
return table[row][col];
}
}
