Eth*_*han 3 java arraylist out-of-memory
我想用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);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索过了OutOfMemoryError,我已经将Eclipse的初始堆大小增加到2G :( -Xms2g -Xmx4g -Xss2m它被传递给JVM).但这并没有解决问题.
然后我想也许我应该List<Integer> edge通过调用垃圾收集变量,System.gc()以防它的内存没有被清除.那也行不通.
我想也许问题出在List<List<Integer>>数据结构上.我试过了List<int[]>,持续了一段时间:在OutOfMemoryError发生之前添加了更多的边缘.我现在没有更好的主意.
我一直在寻找类似的问题,但没有找到太多帮助.我想知道是否有人有这种情况的经验.