简体   繁体   中英

Creating a temp file is incredibly slow

Any reason why a call to

File.createTempFile("prefix", ".suffix", new File("C:\\");

might take 40-50 seconds to complete?

Update: I knocked up a little test harness that benchmarks creating 100 test files on C:\\ and the default tmp folder. Specifying "C:\\" is consistently ~0.9ms slower than just leaving it on the default, allowing for JVM warmup time, GC pauses etc. (No clue why this should be, but its not a problem.)

Not a single run suffered from anything like that level of delay, which suggests the app is doing something else first which is causing the problem.

Using Suns JVM 1.6.0_12 client.

Time ago when developing a swing based application I came across a bug in the JVM which will cause the file requester open to be really slow if there is a big zip file on your desktop. And there is also another issue related when a big number of files exists in a folder.

Probably there can be a correlation with your issue. Which version of JDK are you usign ?

Please take a look at this thread for some info:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4638397

http://groups.google.com/group/comp.lang.java.help/browse_thread/thread/ec8854f82f381123

Defrag the disk is also a good idea.

try it:

try {
        // Create temp file.
        File temp = File.createTempFile("pattern", ".suffix");

        // Delete temp file when program exits.
        temp.deleteOnExit();

        // Write to temp file
        BufferedWriter out = new BufferedWriter(new FileWriter(temp));
        out.write("aString");
        out.close();
    } catch (IOException e) {
    }

I've seen file deletions on Windows take as long as a minute, but not file creation. I'd check to make sure you've defragged recently, and also that you have a reasonable number of files in your home. Once you get past 1,000 files (including hidden ones) Windows has a real hard time.

What happens if you don't specify c:\\ and allow Java to place the file in it's default location?

Virus checkers can sometimes make filesystem access slow, particularly on Windows systems. They intercept all access to the filesystem and can do significant processing before allowing applications to write or read from the disk.

I'd check for and disable any virus checking software and see if that helps.

如果其他建议没有帮助(禁用病毒扫描程序并检查间谍软件),则建议您获取JDK源代码并运行IDE的调试器,以查看在createTempFile()期间它“挂在哪里”。

FWIW,我最终不得不运行磁盘清理

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM