我已经在 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);
}