2 java windows io performance file
我正在尝试使用Java 5.0 x64(在Windows XP上)执行大文件(~4GB)的一次性读取.
最初文件读取速度非常快,但逐渐吞吐量大幅减慢,随着时间的推移,我的机器似乎反应迟钝.
我已经使用ProcessExplorer监视文件I/O统计信息,看起来该进程最初读取500MB /秒,但这个速率逐渐降低到大约20MB /秒.
关于维护文件I/O速率的最佳方法的任何想法,特别是使用Java读取大文件?
这是一些测试代码,显示"间隔时间"继续增加.只需传递一个至少500MB的文件.
import java.io.File;
import java.io.RandomAccessFile;
public class MultiFileReader {
public static void main(String[] args) throws Exception {
MultiFileReader mfr = new MultiFileReader();
mfr.go(new File(args[0]));
}
public void go(final File file) throws Exception {
RandomAccessFile raf = new RandomAccessFile(file, "r");
long fileLength = raf.length();
System.out.println("fileLen: " + fileLength);
raf.close();
long startTime = System.currentTimeMillis();
doChunk(0, file, 0, fileLength);
System.out.println((System.currentTimeMillis() - startTime) + " ms");
}
public void doChunk(int threadNum, File file, long start, long end) throws Exception {
System.out.println("Starting partition " + start + " to " + end);
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(start);
long cur = start;
byte buf[] = new byte[1000];
int lastPercentPrinted = 0;
long intervalStartTime = System.currentTimeMillis();
while (true) {
int numRead = raf.read(buf);
if (numRead == -1) {
break;
}
cur += numRead;
if (cur >= end) {
break;
}
int percentDone = (int)(100.0 * (cur - start) / (end - start));
if (percentDone % 5 == 0) {
if (lastPercentPrinted != percentDone) {
lastPercentPrinted = percentDone;
System.out.println("Thread" + threadNum + " Percent done: " + percentDone + " Interval time: " + (System.currentTimeMillis() - intervalStartTime));
intervalStartTime = System.currentTimeMillis();
}
}
}
raf.close();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
Jon*_*eet 10
我非常怀疑你的磁盘每秒真正得到500MB.有可能是操作系统缓存了数据 - 而且每秒20MB是真正碰到磁盘时发生的事情.
这很可能在Vista资源管理器的磁盘部分中可见 - 而低技术的方式是监听磁盘驱动器:)