Java File.createNewFile() 比 FileOutputStream 慢得多



我观察到在使用File.createNewFile()或File.createTempFile()时出现了有趣的性能下降。 以下代码创建 48 个线程,每个线程将大约 128MB 的数据写入不同的文件。如果我按原样运行代码,则在我的特定计算机上大约需要 60 秒。 如果我完全按原样运行代码,除了我注释掉 f.createTempFile() 调用,那么大约需要 5 秒。

import java.util.*;
import java.util.concurrent.*;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public final class TestFile implements Runnable {
public void run() {
byte[] b = new byte[128205100];
Arrays.fill(b, (byte)10);
try {
File f = new File("/tmp/test", UUID.randomUUID().toString());
// If I comment the following f.createNewFile() then the code takes
// 5 seconds rather than 60 to execute. 
f.createNewFile();
FileOutputStream fOutputStream =  new FileOutputStream(f);
BufferedOutputStream fBufStream = new BufferedOutputStream(fOutputStream, 32768);
fBufStream.write(b);
fBufStream.close();
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
}
public static void main(String[] args) {
final ExecutorService executorPool = Executors.newFixedThreadPool(48);
for (int counter=0; counter < 48; counter++) {
executorPool.execute(new TestFile());
}
try {
executorPool.shutdown();
executorPool.awaitTermination(120, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.err.println("Caught InterruptedException: " + e.getMessage());
}       
}
}

使用 jstack,我可以看到,当在所有线程上方运行代码时,它们的大部分时间最终都花在 close0() 上。 不幸的是,这个函数是原生的:-/知道我在哪里找到它的来源吗?

"Thread-47" #68 prio=5 os_prio=0 tid=0x00007f21001de800 nid=0x4eb4 runnable [0x00007f209edec000]
java.lang.Thread.State: RUNNABLE
at java.io.FileOutputStream.close0(Native Method)
at java.io.FileOutputStream.access$000(FileOutputStream.java:53)
at java.io.FileOutputStream$1.close(FileOutputStream.java:356)
at java.io.FileDescriptor.closeAll(FileDescriptor.java:212)
- locked <0x00000005908ad628> (a java.io.FileDescriptor)
at java.io.FileOutputStream.close(FileOutputStream.java:354)
at java.io.FilterOutputStream.close(FilterOutputStream.java:159)
at TestFile.run(TestFile.java:19)
at java.lang.Thread.run(Thread.java:745)

我的猜测是某个地方(在本机 close0 内)有人正在发布同步,但我没有找到它。 我已经在几台机器上对此进行了测试,在其中一些机器中我没有看到退化。 所以这可能是基于配置或环境的。

我正在使用Java 8在Ubuntu上运行。

任何帮助将不胜感激。 谢谢!

这很简单。File.createNewFile()按该名称搜索文件,如果文件不存在,则创建一个新文件,或者失败,您正确地忽略了该文件,因为它是否成功并不重要。new FileOutputStream()搜索任何同名的现有文件,将其删除,然后创建一个新文件。

因此,很明显,File.createNewFile()后面跟着new FileOutputStream()完全是浪费时间,因为它迫使操作系统:

  1. 搜索文件。
  2. 如果它不存在,请创建它,或者失败。
  3. 搜索文件。
  4. 如果存在,请将其删除。
  5. 创建它。

显然(1)和(2)是浪费时间,强迫(4)在可能不需要的时候发生。

解决方案:在new FileOutputStream(...)之前不要打电话给File.createNewFile()。或者new FileWriter(...)就此而言,或者new PrintStream/PrintWriter(...)两者兼而有之。没有什么可得到的,时间和空间可以浪费。

最新更新