开发者

Printing rows, Java heap space [closed]

开发者 https://www.devze.com 2023-03-02 17:26 出处:网络
开发者_Python百科This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not
开发者_Python百科 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号