Java – sometimes it takes 45 seconds to close RandomAccessFile

In my program, close Java util. RandomAccessFile sometimes takes 45 seconds (almost exactly between 44.998 and 45.003 seconds) The program creates and closes a large number of small files Closing files is usually very fast (between 0 and 0.1 seconds) If I debug the program, it will be pasted in the local method RandomAccessFile In close0

The same problem occurs when using fileoutputstream instead of RandomAccessFile (in this case, the program is blocked in the local method fileoutputstream.close0)

Someone has an idea. What might it be? Can you reproduce the problem on the system (I can only reproduce it on MAC, not on Windows XP, and I haven't tested it on Linux yet)?

Update 2:

This only happens on Mac OS X I use JDK 1.6 0_ 22-b04. It occurs in 32 - bit and 64 - bit It doesn't seem to happen on Windows XP

My test case is:

import java.io.File;
import java.io.RandomAccessFile;
public class TestFileClose {
    public static void main(String... args) throws Exception {
        for (int i = 0; i < 100000; i++) {
            String name = "test" + i;
            RandomAccessFile r = new RandomAccessFile(name,"rw");
            r.write(0);
            long t = System.currentTimeMillis();
            r.close();
            long close = System.currentTimeMillis() - t;
            if (close > 200) {
                System.out.println("closing " + name +
                        " took " + close + " ms!");
            }
            if (i % 2000 == 0) {
                System.out.println("test " + i + "/100000");
            }
            new File(name).delete();
        }
    }
}

Output example on my machine:

test 0/100000
test 2000/100000
test 4000/100000
test 6000/100000
test 8000/100000
test 10000/100000
closing test10030 took 44998 ms!
test 12000/100000
test 14000/100000
test 16000/100000
closing test16930 took 44998 ms!
test 18000/100000
test 20000/100000

Solution

This may be McAfee antivirus software installed on my machine I have to install it... But if press access scanning is disabled, the problem will also be displayed

In order to verify that it is not anti-virus, someone repeated the test on his machine (without anti-virus) and got the same problem, I guess

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>