整数を含む大きなtxtファイルがあります。ファイルの各行には、空白で区切られた 2 つの整数があります。ファイルのサイズは 63 Mb です。
Pattern p = Pattern.compile("\\s");
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = p.split(line);
String s1 = new String(tokens[0]);
String s2 = new String(tokens[1]);
int startLabel = Integer.valueOf(s1) - 1;
int endLabel = Integer.valueOf(s2) - 1;
Vertex fromV = vertices.get(startLabel);
Vertex toV = vertices.get(endLabel);
Edge edge = new Edge(fromV, toV);
fromV.addEdge(edge);
toV.addEdge(edge);
edges.add(edge);
System.out.println("Edge from " + fromV.getLabel() + " to " + toV.getLabel());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:2694)
at java.lang.String.<init>(String.java:203)
at java.lang.String.substring(String.java:1913)
at java.lang.String.subSequence(String.java:1946)
at java.util.regex.Pattern.split(Pattern.java:1202)
at java.util.regex.Pattern.split(Pattern.java:1259)
at SCC.main(SCC.java:25)
なぜこの例外が発生するのですか? コードを変更して回避するにはどうすればよいですか?
編集: 私はすでにヒープ サイズを 2048m に増やしています。それを消費しているのは何ですか?それは私も知りたいです。
私が知っている限り、jvmは頂点のリスト、エッジのセット、バッファリングされたリーダーのバッファ、および1つの小さな文字列「行」にメモリを割り当てる必要があります。この outOfMemory がどこから来ているのかわかりません。
string.split() メソッドについて読みました。メモリリークが発生していると思いますが、どうすればよいかわかりません。