Possible file descriptor leakage in RandomAccessFile

Robert Larsen robert at komogvind.dk
Tue Jun 9 06:25:52 PDT 2009


Hi

I have a number of servers running and I have discovered a problem when
turning on a lot of logging.

I have a logging mechanism that writes log entries to a file and when
the file reaches a certain size it is closed and another one opened.

It is implemented like this:

    public static class LogFileService {
        private static LogFileService instance;
        private HashMap<String, RandomAccessFile> fileMap;

        private LogFileService() {
            fileMap = new HashMap<String, RandomAccessFile>();
        }

        public void write(String path, String msg) {
            try {
                RandomAccessFile f = getFile(path);
                f.writeBytes((new Date()) + " " + msg + "\n");
                if (f.getFD().valid() == false || f.getFilePointer() >=
Log.MAX_FILE_SIZE) {
                    f.close();
                    fileMap.remove(path);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private RandomAccessFile getFile(String path) throws IOException {
            RandomAccessFile file = fileMap.get(path);

            if (file == null || file.getFD().valid() == false) {
                int count = 0;
                String newPath = null;
                do {
                    newPath = String.format(path + "%1$03d", count++);
                } while ((new File(newPath)).exists());
                file = new RandomAccessFile(newPath, "rw");
                fileMap.put(path, file);
            }

            return file;
        }

        public static LogFileService getInstance() {
            if (instance == null) {
                instance = new LogFileService();
            }
            return instance;
        }
    }


But with lots of logging the servers crash with too many open files and
listing the open files in /proc/pid/fd (this is on Linux) I can see
hundreds of open log files. That shouldn't be the case so I dumped the
memory of the Java process and looked at it through 'jhat'. Only one
instance of RandomAccessFile as I expected.

But the files are still opened by the process.

Anybody know anything about this ?

This is with 1.6.0_12 on a 64 bit system.

Best regards,
Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20090609/a0cf8e29/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
Url : http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20090609/a0cf8e29/attachment.bin 


More information about the nio-dev mailing list