java内存不足时读取文件,我读取大文本文件的Java程序内存不足,任何人都可以帮忙解释原因吗?...

I have a large text file with 20 million lines of text. When I read the file using the following program, it works just fine, and in fact I can read much larger files with no memory problems.

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");

}

}

However if I need to append some records to this file before reading it, the BufferedReader consumes a huge amount of memory (I have just used Windows task manager to monitor this, not very scientific I know but it demonstrates the problem). The amended program is below, which is the same as the first one, except I am appending a single record to the file first.

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");

}

}

A screenshot of Windows task manager, where the large bump in the line shows the memory consumption when I run the second version of the program.

YaZyJ.png

So I was able to read this file without running out of memory. But I have much larger files with more than 50 million records, which encounter an out of memory exception when I run this program against them? Can someone explain why the first version of the program works fine on files of any size, but the second program behaves so differently and ends in failure? I am running on Windows 7 with:

java version "1.7.0_05"

Java(TM) SE Runtime Environment (build 1.7.0_05-b05)

Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)

解决方案

you can start a Java-VM with VM-Options

-XX:+HeapDumpOnOutOfMemoryError

this will write a heap dump to a file, which can be analysed for finding leak suspects

Use a '+' to add an option and a '-' to remove an option.

If you are using Eclipse the Java Memory Analyzer Plugin MAT to get Heap-Dumps from running VMs with some nice analyses for Leak Suspects etc.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值