0

我已经在 Eclipse 中编写了一个代码,它可以正常运行小输入值,但是一旦我的测试用例大小增加,我就会OutOfMemoryException出错StackOverFlow

我试图用来eclipse.exe -vmargs -Xmx1g让我的堆输出到 1G,但我仍然得到同样的错误。当我尝试 2G 时,它说无法启动 JVM。

所以我想知道是否有任何方法可以让我运行这段代码。任何帮助,将不胜感激。提前致谢。

编辑:这是我的堆溢出的地方。输入样本太大并导致内存问题。

 while ((line = br.readLine()) != null) {

        String[] linevalue= (line.trim().split("\\s+"));
        int l= linevalue.length;
        dg.addNode(Long.parseLong(linevalue[0]));
        dg.addNode(Long.parseLong(linevalue[1]));
        dg.addEdge(Long.parseLong(linevalue[0]), Long.parseLong(linevalue[1]));

    }

在另一个类中,存在以下代码,这里 mGraph 是一个 HashMap。

public boolean addNode(T node) {
    /* If the node already exists, don't do anything. */
    if (mGraph.containsKey(node))
        return false;

    /* Otherwise, add the node with an empty set of outgoing edges. */
    mGraph.put(node, new HashSet<T>());
    return true;
}


public void addEdge(T start, T dest) {
    /* Confirm both endpoints exist. */
    if (!mGraph.containsKey(start) || !mGraph.containsKey(dest))
        throw new NoSuchElementException("Both nodes must be in the graph.");

    /* Add the edge. */
    mGraph.get(start).add(dest);
}
4

2 回答 2

1

在 Eclipse 中,您可以在执行代码时设置 VM 的大小。

Run > Run configurations。然后在选项卡Arguments中,放在-Xms1000mVM 参数下。

于 2012-04-16T20:37:42.173 回答
0

跟进 tskuzzy 的回答

对于堆,

-Xms -Xmx

对于堆栈

-Xss

我会1g推荐XmxXmx,8m-Xss

于 2012-04-16T20:54:34.210 回答