Printing rows, Java heap space [closed]
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.
LineNumberReader
extends BufferedReader
. 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:
LineNumberReader lnReader = new LineNumberReader(fReader);
with:
LineNumberReader lnReader = new LineNumberReader(fReader, 4096);
use this class to read the file: RandomAccessFile then you won't have the out of memory problem anymore
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
精彩评论