tags:

views:

174

answers:

5

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.

A: 

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) {
    }
tommaso
Have done - updated the question. Thanks!
MHarris
+1  A: 

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?

brianegge
+1  A: 

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.

Matt Ryall
+1  A: 

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.

dawez
There was a 4gb zip file on the root (don't ask). Moving it into a subfolder consistently gives times of ~1.5s for the check.
MHarris
It seems that this bug is something that we have to live with and it's reappearing from time to time just to spice up developers' life.
dawez
A: 

If other suggestions doesn't help (disable virusscanners and check for spyware), then I'd suggest to go get the JDK source code and run the IDE's debugger to see where it "hangs" during createTempFile().

BalusC