我想用 Java 创建一个非常大的图(有大约 1000 万条边)。我打算List<List<Integer>>
描述边缘,用里面List<Integer>
描述每条边的两个顶点(并且顶点是整数类型)。
以下代码抛出 OutOfMemoryError
在将大约 100 万条边添加到图中之后。 (为了便于讨论,我简化了边缘的生成方式。)
public static void main(String[] args) {
List<List<Integer>> graph = new ArrayList<List<Integer>>();
for (int i = 0; i < 10000000; i++) {
List<Integer> edge = new ArrayList<Integer>();
// the real edges are more complicated (than from vertex i to vertex i+1)
// this is simplified for the sake of the discussion here
edge.add(i);
edge.add(i+1);
graph.add(edge);
}
}
我搜索了OutOfMemoryError
,并且我已将 Eclipse 的初始堆大小增加到 2G:-Xms2g -Xmx4g -Xss2m
(传递给 JVM)。但这并没有解决问题。
然后我想也许我应该对 List<Integer> edge
进行垃圾回收变量,通过调用 System.gc()
,以防它的内存没有被清除。那也不管用。
我在想问题可能出在 List<List<Integer>>
上数据结构。我试过 List<int[]>
,持续时间更长:在 OutOfMemoryError
之前添加了更多边发生。我现在没有更好的主意。
我已经搜索过类似的问题,但没有找到太多帮助。不知有没有人遇到过这种情况。
最佳答案
让您的程序使用 Eclipse 的更多内存:
转到运行 -> 运行配置。你会看到这个窗口
点击参数
将您的参数输入到 VM
关于Java OutOfMemoryError 与 ArrayList<List<Integer>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533263/