Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a RandomAccessFile sometimes takes exactly 45 seconds

In my program, closing a java.util.RandomAccessFile sometimes takes exactly 45 seconds (well, almost exactly: between 44.998 and 45.003 seconds). The program creates and closes lots of small files. Usually closing the file is very quick (between 0 and 0.1 seconds). If I debug the program, it's stuck in the native method RandomAccessFile.close0.

The same problem also occurs when using FileOutputStream instead of RandomAccessFile (in which case the program is blocked in the native method FileOutputStream.close0).

Has somebody an idea what that could be? Can you reproduce the problem on your system (I can reproduce it only on a Mac, not on Windows XP; I didn't test yet on Linux)?


Update 2:

This only seems to happend on Mac OS X. I use JDK 1.6.0_22-b04. It happens on both 32-bit and 64-bit. On Windows XP it doesn't seem to occur.

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();
        }
    }
}

Example output 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
like image 200
Thomas Mueller Avatar asked Jan 21 '11 15:01

Thomas Mueller


People also ask

What is the use of RandomAccessFile?

Java RandomAccessFile provides the facility to read and write data to a file. RandomAccessFile works with file as large array of bytes stored in the file system and a cursor using which we can move the file pointer position.

Which method in the RandomAccessFile class provides for random access?

RandomAccessFile Class provides a way to random access files using reading and writing operations. It works like an array of byte storted in the File. Syntax : public int read() Parameters : -------- Return : reads byte of data from file, -1 if end of file is reached.

What is the difference between the file and RandomAccessFile classes?

File is an abstract representation of a file/directory which may or may not even exist. It doesn't consume any resources, so you can store them as much as you want. RandomAccessFile is for actual file access (reading, seeking, writing), so you don't need it here.

How do I turn off RandomAccessFile?

The java. io. RandomAccessFile. close() method closes this random access file stream and releases any system resources associated with the stream.


1 Answers

In my case, it turned out to be McAfee antivirus installed on my machine. I had to install it (company policy)...

The problem also showed up if I disabled the on-access scan.

like image 77
Thomas Mueller Avatar answered Sep 20 '22 06:09

Thomas Mueller