java - 尝试设置数组元素时出现 "java.lang.OutOfMemoryError: Java heap space"

标签 java out-of-memory

这是我在 Google 的 antai 挑战中使用的一段代码,每当我运行它时,它似乎进入了某个无限循环,然后我在底部得到了堆栈跟踪。看着这段代码,我精疲力尽了,我这辈子都想不通。

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)

感谢任何帮助

最佳答案

您需要进行一些调试/清理。从快速 View 中我看到,在 assesRoute() 中你 永远不会操纵 interesetedNode tile - 这个循环不会正常结束。

最好将访问过的节点保留在哈希集中 - 您只需要确保存在或不存在,而不是节点的数量。替代方案是节点本身中的 boolean 标志,这样您就可以使用一个列表。

关于java - 尝试设置数组元素时出现 "java.lang.OutOfMemoryError: Java heap space",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8530577/

相关文章:

java - 如何查找系统是否已连接网络摄像头

java - 在矩形上使用 AffineTransform

java - 加密属性文件中的值

java - 读取外部文本文件并存储到数组中

java - 需要了解这段java代码的工作原理

r - 在 R Studio 中,我遇到 Java 内存不足(对于 RWeka)

c++ - C++ 生成器中的 "[ilink32] Fatal: Out of memory"

java - 为什么我的程序在字节数组中保存 1.5Gb 内存?

exception - 处理 "java.lang.OutOfMemoryError: PermGen space"错误

redis - 如果使用混淆库,Microsoft.Web.RedisSessionStateProvider 的内存不足异常