打印行,Java 堆空间
我想打印一个巨大的文本文件(超过 600 000 MB)中的每一行。
但是,当我尝试下面的代码时,我在到达行号 1 000 000 之前收到“...OutOfMemoryError: Java heap space”。
是否有比 FileReader 和 LineNumberReader 更好的方法来处理输入?
FileReader fReader = new FileReader(new File("C:/huge_file.txt"));
LineNumberReader lnReader = new LineNumberReader(fReader);
String line = "";
while ((line = lnReader.readLine()) != null) {
System.out.println(lnReader.getLineNumber() + ": " + line);
}
fReader.close();
lnReader.close();
提前致谢!
谢谢大家的回答!
我终于发现了内存泄漏,一个未使用的 java 类实例,它在每行迭代中都会自我复制。换句话说,它与文件加载部分无关。
I want to print each line from a huge textfile (more than 600 000 MB).
But when I try the code below I get "...OutOfMemoryError: Java heap space" right before reaching line number 1 000 000.
Is there a better way to handle the input rather than FileReader and LineNumberReader?
FileReader fReader = new FileReader(new File("C:/huge_file.txt"));
LineNumberReader lnReader = new LineNumberReader(fReader);
String line = "";
while ((line = lnReader.readLine()) != null) {
System.out.println(lnReader.getLineNumber() + ": " + line);
}
fReader.close();
lnReader.close();
Thanks in advance!
Thanks all for your answers!
I finally found the memory leak, an unused java class instance which duplicated it self for each row iteration. In other words, it had nothing to do with the file loading part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LineNumberReader
扩展了BufferedReader
。可能是缓冲读取器缓冲过多。通过分析器运行程序应该毫无疑问地证明了这一点。BufferedReader 的构造函数之一采用缓冲区大小,该构造函数也可在行号读取器中使用。
替换:
为:
LineNumberReader
extendsBufferedReader
. It may be that the buffered reader is buffering too much. Running the program through a profiler should prove this without a doubt.One of the constructors of the
BufferedReader
takes a buffer size, this constructor is also available in the line number reader.replace:
with:
使用此类读取文件: RandomAccessFile 那么你就不会再遇到内存不足的问题了
use this class to read the file: RandomAccessFile then you won't have the out of memory problem anymore
也许您应该尝试设置 Java 虚拟机的最大堆大小?
或者检查此链接:
http://www. techrepublic.com/article/handling-large-data-files-efficiently-with-java/1046714
Maybe you should try setting the max heap size for Java virtual machine?
Or check this link:
http://www.techrepublic.com/article/handling-large-data-files-efficiently-with-java/1046714