我写的外部排序整理磁盘外部排序GC开销
大2演出文件我首先拆分文件到装入内存,并单独排序每一个数据块,并重写他们回磁盘。但是,在这个过程中,我在函数geModel的String.Split方法中获取GC内存开销异常。以下是我的代码。
private static List<Model> getModel(String file, long lineCount, final long readSize) {
List<Model> modelList = new ArrayList<Model>();
long read = 0L;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
//Skip lineCount lines;
for (long i = 0; i < lineCount; i++)
br.readLine();
String line = "";
while ((line = br.readLine()) != null) {
read += line.length();
if (read > readSize)
break;
String[] split = line.split("\t");
String curvature = (split.length >= 7) ? split[6] : "";
String heading = (split.length >= 8) ? split[7] : "";
String slope = (split.length == 9) ? split[8] : "";
modelList.add(new Model(split[0], split[1], split[2], split[3], split[4], split[5], curvature, heading, slope));
}
br.close();
return modelList;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static void split(String inputDir, String inputFile, String outputDir, final long readSize) throws IOException {
long lineCount = 0L;
int count = 0;
int writeSize = 100000;
System.out.println("Reading...");
List<Model> curModel = getModel(inputDir + inputFile, lineCount, readSize);
System.out.println("Reading Complete");
while (curModel.size() > 0) {
lineCount += curModel.size();
System.out.println("Sorting...");
curModel.sort(new Comparator<Model>() {
@Override
public int compare(Model arg0, Model arg1) {
return arg0.compareTo(arg1);
}
});
System.out.println("Sorting Complete");
System.out.println("Writing...");
writeFile(curModel, outputDir + inputFile + count, writeSize);
System.out.println("Writing Complete");
count++;
System.out.println("Reading...");
curModel = getModel(inputDir + inputFile, lineCount, readSize);
System.out.println("Reading Complete");
}
}
它使它通过一次并从文件中排序〜250 MB的数据。但是,在第二次传递时,它会在String.split函数上引发GC Memory Overhead异常。我不想使用外部库,我想自己学习。排序和分裂工程,但我不明白为什么GC在string.split函数抛出内存开销异常。
你可以发布每行有什么样的数据。正如Bill在他的回答中提到的,您可以通过多种方式进行调试或分析。有一件事我不明白的是Model对象/类正在做什么。基于我的理解。您的文件似乎是由制表符分隔的单词,并且您很可能每行只有8个单词。 – Acewin
您需要查看如何正确进行外部排序。这不是。你需要做的替代选择初始运行,随后进行多相或合并均衡分布。 – EJP
@EJP我知道还有更多的方法来优化这种排序。但是,这种确实给了我排序的数据。输出很好,但可能不如具有多个合并阶段的实际外部排序那样优化。这一个只有一个合并阶段,它有更多的IO操作,而不是优化的合并排序。你会同意我写的吗? – jLangley011