【发布时间】:2015-07-01 21:28:32
【问题描述】:
我正在尝试预处理一个大的 txt 文件 (10G),并将其存储在二进制文件中以备将来使用。随着代码的运行,它会变慢并以
结尾线程“主”java.lang.OutOfMemoryError 中的异常:GC 开销 超出限制
输入文件的结构如下
200020000000008;0;2
200020000000004;0;2
200020000000002;0;2
200020000000007;1;2
这是我正在使用的代码:
String strLine;
FileInputStream fstream = new FileInputStream(args[0]);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
//Read File Line By Line
HMbicnt map = new HMbicnt("-1");
ObjectOutputStream outputStream = null;
outputStream = new ObjectOutputStream(new FileOutputStream(args[1]));
int sepIndex = 15;
int sepIndex2 = 0;
String str_i = "";
String bb = "";
String bbBlock = "init";
int cnt = 0;
lineCnt = 0;
while ((strLine = br.readLine()) != null) {
//rozparsovat radek
str_i = strLine.substring(0, sepIndex);
sepIndex2 = strLine.substring(sepIndex+1).indexOf(';');
bb = strLine.substring(sepIndex+1, sepIndex+1+sepIndex2);
cnt = Integer.parseInt(strLine.substring(sepIndex+1+sepIndex2+1));
if(!bb.equals(bbBlock)){
outputStream.writeObject(map);
outputStream.flush();
map = new HMbicnt(bb);
map.addNew(str_i + ";" + bb, cnt);
bbBlock = bb;
}
else{
map.addNew(str_i + ";" + bb, cnt);
}
}
outputStream.writeObject(map);
//Close the input stream
br.close();
outputStream.writeObject(map = null);
outputStream.close();
基本上,它通过 in 文件并将数据存储到对象 HMbicnt(这是一个哈希映射)。一旦在第二列中遇到新值,它应该将对象写入输出文件,释放内存并继续。
感谢您的帮助。
【问题讨论】:
-
要么 bblocks 从不重复,导致你不断添加到同一个地图,要么
HMbicnt的实现在做一些可疑的事情。你能展示addNew()的实现以及它调用的任何方法吗?
标签: java garbage-collection hashmap