我有一个包含 2000 万行文本的大型文本文件。当我使用以下程序读取文件时,它工作得很好,事实上我可以读取更大的文件而不会出现内存问题。
public static void main(String[] args) throws IOException {
File tempFile = new File("temp.dat");
String tempLine = null;
BufferedReader br = null;
int lineCount = 0;
try {
br = new BufferedReader(new FileReader(tempFile));
while ((tempLine = br.readLine()) != null) {
lineCount += 1;
}
} catch (Exception e) {
System.out.println("br error: " +e.getMessage());
} finally {
br.close();
System.out.println(lineCount + " lines read from file");
}
}
但是,如果我需要在读取该文件之前附加一些记录,BufferedReader 会消耗大量内存(我刚刚使用 Windows 任务管理器来监视这一点,我知道这不是很科学,但它说明了问题)。修改后的程序如下,与第一个程序相同,只是我先向文件附加一条记录。
public static void main(String[] args) throws IOException {
File tempFile = new File("temp.dat");
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(tempFile, true)));
pw.println(" ");
} catch (Exception e) {
System.out.println("pw error: " + e.getMessage());
} finally {
pw.close();
}
String tempLine = null;
BufferedReader br = null;
int lineCount = 0;
try {
br = new BufferedReader(new FileReader(tempFile));
while ((tempLine = br.readLine()) != null) {
lineCount += 1;
}
} catch (Exception e) {
System.out.println("br error: " +e.getMessage());
} finally {
br.close();
System.out.println(lineCount + " lines read from file");
}
}
Windows 任务管理器的屏幕截图,其中线条中的大凸起显示了我运行该程序的第二个版本时的内存消耗。
所以我能够读取这个文件而不会耗尽内存。但是我有更大的文件,有超过 5000 万条记录,当我对它们运行这个程序时,会遇到内存不足的异常吗?有人可以解释为什么该程序的第一个版本在任何大小的文件上都可以正常工作,但第二个程序的行为却如此不同并以失败告终?我在 Windows 7 上运行:
java版本“1.7.0_05”
Java(TM) SE 运行时环境(版本 1.7.0_05-b05)
Java HotSpot(TM) 客户端 VM(内部版本 23.1-b03,混合模式,共享)
你可以启动一个Java虚拟机VM选项 http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
-XX:+HeapDumpOnOutOfMemoryError
这会将堆转储写入文件,可以分析该文件以查找泄漏嫌疑人
使用“+”添加选项,使用“-”删除选项。
如果您使用 Eclipse Java 内存分析器插件MAT http://www.eclipse.org/mat/从正在运行的虚拟机中获取堆转储,并对泄漏嫌疑人等进行一些很好的分析。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)