当我运行我的8拼图程序时,一直出现“GC overhead limit exceeded”的提示。我尝试给JVM添加更多内存,但没有帮助。
以下是有问题的方法:
以下是有问题的方法:
public void search() {
addToQueue(start, null);// add root
while (!queue.isEmpty()) {
currState = queue.removeFirst();
if (goal.equals(currState)) {
solution = true;
printSolution(currState);
break;
} else {
a = currState.indexOf("0");
// left
while (a != 0 && a != 3 && a != 6) {
String nextState = currState.substring(0, a - 1) + "0"
+ currState.charAt(a - 1)
+ currState.substring(a + 1);
addToQueue(nextState, currState);
nodes++;
break;
}
// up
while (a != 0 && a != 1 && a != 2) {
String nextState = currState.substring(0, a - 3) + "0"
+ currState.substring(a - 2, a)
+ currState.charAt(a - 3)
+ currState.substring(a + 1);
addToQueue(nextState, currState);
nodes++;
break;
}
// right
while (a != 2 && a != 5 && a != 8) {
String nextState = currState.substring(0, a)
+ currState.charAt(a + 1) + "0"
+ currState.substring(a + 2)
+ currState.substring(a + 1);
addToQueue(nextState, currState);
nodes++;
break;
}
// down
while (a != 6 && a != 7 && a != 8) {
String nextState = currState.substring(0, a)
+ currState.substring(a + 3, a + 4)
+ currState.substring(a + 1, a + 3) + "0"
+ currState.substring(a + 4);
addToQueue(nextState, currState);
nodes++;
break;
}
}
}
}
Start 是我从 .txt 文件中读取的字符串。对于某些问题它可以正常工作,但是某些情况下会产生这个错误。
private void addToQueue(String newState, String oldState) {
if (!levelDepth.containsKey(newState)) {
newValue = oldState == null ? 0 : levelDepth.get(oldState) + 1;
unique++;
levelDepth.put(newState, newValue);
queue.add(newState);
stateHistory.put(newState, oldState);
}
}