Possible file descriptor leakage in RandomAccessFile

Alan Bateman Alan.Bateman at Sun.COM
Tue Jun 9 07:14:03 PDT 2009


Robert Larsen wrote:
> 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
This doesn't seem to New I/O or OpenJDK related so I would suggest 
forums.java.sun.com as a good place to seek help. FWIW, RandomAccessFile 
doesn't have a finalizer so files aren't closed when GC'ed. From a brief 
glance at the code fragment I wonder if this was being used by several 
threads. It doesn't seem to have any synchronization and so several 
threads can open the same file at around the same time, which may be the 
source of the problem (among others).

-Alan.



More information about the nio-dev mailing list