A Look on "Too many open files" Exception

  • This exception occurs when you are opening a file (or a pipe or a socket) but the number of open file descriptors count already reached to a limit. You can verify this by printing the stacktrace for the IOException.
  • Whenever a file/socket is opened, the operating system creates an entry to represent this file and stores its information. Each entry is represented by an integer value and this entry is termed as file descriptor.
  • Basically, Java class FileDescriptor provides a handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes.
  • The applications should not create FileDescriptor objects, they are mainly used in creation of FileInputStream orFileOutputStream objects to contain it.

Check the open files of a process

# lsof -p 12390 | wc -l

Tuning file descriptor limits on Linux

  • Linux limits the number of file descriptors that any one process may open.
  • The default limits are 1024 open files per process.
  • The command ulimit -aS displays the current limit, and ulimit -aH displays the hard limit (above which the limit cannot be increased without tuning
    kernel parameters in /proc).
  • The file descriptor limit can be increased using the following procedure:
Edit /etc/security/limits.conf and add the lines:
      *       soft    nofile  1024
      *       hard    nofile  65535
This will increase the limits for all users of the machine, if you want to do this for a specific user replace * with that username.

Find open files limit per process (may be different for different processes)

# ulimit -n

Count all opened files by all process

# cat /proc/sys/fs/file-nr

Get maximum open files count allowed

# cat /proc/sys/fs/file-max

Open file limit applied to a specific process while running

# cat /proc/<pid>/limits

Testing code for this is available: ExceptionsTest