2011-12-16 35 views
1

這是我用於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) 

任何幫助表示讚賞

+2

您還沒有向我們展示您的Tile代碼 - 特別是構造函數。 – 2011-12-16 06:31:20

+1

您是否嘗試過使用探查器並查看哪些對象在泄漏? VishalVM隨JDK一起提供。 – 2011-12-16 06:36:45

回答

1

我不知道這個谷歌挑戰是什麼,所以這可能沒有多大用處(即我不知道你是否有權訪問JVM),雖然它是一種通用的方法來幫助診斷這種類型的問題。

我建議打開JVM標誌-XX:+HeapDumpOnOutOfMemoryError,然後再次運行您的代碼。 OutOfMemoryError將導致堆轉儲。然後,您可以使用諸如Eclipse MAT之類的內容來對此進行分析。

0

的MyBot.doTurn()在堆棧跟蹤表明你的程序是運行在或發現一些種類的路徑...你確定這是一個有限的算法嗎?

2

看起來你創建的對象超出你的內存允許的範圍,猜測你的代碼包含一個不確定的循環,你可以創建一個靜態整數來計算setNeighbors被調用的次數,它創建新對象的例程,並在你的類的主要調用周圍的try/catch的catch語句中顯示此整數。

0

assesRoute是一個while循環。你期望有多少次迭代?打印出實際完成的數量。如果差異很大,則知道存在問題。採取這個迭代的開始狀態,並試圖瞭解爲什麼這個狀態不會在適當的時間結束循環。

3

你需要做一些調試/清理。從快速看,我看到,在assesRoute()你 永遠不會操縱interesetedNode瓷磚 - 此循環不會正常結束。

在散列集中保留訪問節點也更好 - 您只需確保存在或不存在,而不是多個節點。另一種方式是節點本身的布爾標誌,這樣你可以使用一個列表來處理它。

相關問題