這是我用於Google的antai挑戰的一段代碼,只要我運行它似乎進入一些無限循環然後我在底部獲得堆棧跟蹤。我看着這段代碼已經被燒燬了,我不能爲我的生活弄清楚這一點。「java.lang.OutOfMemoryError:Java堆空間」在嘗試設置數組元素時發生
public class Node
{
private final int xCoord, yCoord;
private int F, G, H;
private Tile location;
Node previousNode;
private Tile [] neighbors;
/*
G
the exact cost to reach this node from the starting node.
H
the estimated(heuristic) cost to reach the destination from here.
F = G + H
As the algorithm runs the F value of a node tells us how expensive we think it will be to reach our goal by way of that node.
*/
public Node(Tile loc)
{
location = loc;
xCoord = location.getCol();
yCoord = location.getRow();
F=G=H=0;
setNeighbors();
}
private void setNeighbors()
{
if(neighbors == null)
{
neighbors = new Tile[4];
}
neighbors[0] = new Tile(xCoord+1,yCoord);
neighbors[1] = new Tile(xCoord-1,yCoord);
neighbors[2] = new Tile(xCoord,yCoord+1);
neighbors[3] = new Tile(xCoord,yCoord-1);//error occurs here!!!!!!
}
}
/**
* Represents a tile of the game map.
*/
public class Tile implements Comparable<Tile> {
private final int row;
private final int col;
/**
* Creates new {@link Tile} object.
*
* @param row row index
* @param col column index
*/
public Tile(int row, int col) {
this.row = row;
this.col = col;
}
/**
* Returns row index.
*
* @return row index
*/
public int getRow() {
return row;
}
/**
* Returns column index.
*
* @return column index
*/
public int getCol() {
return col;
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo(Tile o) {
return hashCode() - o.hashCode();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return row * Ants.MAX_MAP_SIZE + col;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
boolean result = false;
if (o instanceof Tile) {
Tile tile = (Tile)o;
result = row == tile.row && col == tile.col;
}
return result;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return row + " " + col;
}
}
我收到實際的錯誤是:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Node.setNeighbors(Node.java:37)
at Node.<init>(Node.java:25)
at AstarSearch.assessRoute(AstarSearch.java:73)
at MyBot.gatherFood(MyBot.java:153)
at MyBot.doTurn(MyBot.java:124)
at AbstractSystemInputParser.processLine(AbstractSystemInputParser.java:54)
at AbstractSystemInputReader.readSystemInput(AbstractSystemInputReader.java:18)
at MyBot.main(MyBot.java:25)
任何幫助表示讚賞
您還沒有向我們展示您的Tile代碼 - 特別是構造函數。 – 2011-12-16 06:31:20
您是否嘗試過使用探查器並查看哪些對象在泄漏? VishalVM隨JDK一起提供。 – 2011-12-16 06:36:45