Java IO Exception: Too many open files.

From NMSL

java.io.IOException: Too many open files [1]

This indicates that Java Virtual Machine(JVM) tries to open more files than the max-number of file descriptors. The max-number of file descriptors varies by OS: 1024 for Ubuntu linux, 256 for Mac OSX. You can check the limit by typing

$ ulimit -a

A frustrating thing is that the exception still occurs even when there is no way that the number of currently opened files exceeds the limit. This can happen when the Java program opens a new file before JVM closes the old file: it takes longer for JVM to close a file. Hence, if your program keeps opening and closing files without much break, you will most likely experience this exception.

How do we get around with this problem? If you are a super user, the easiest way is too increase the limit of file descriptors. Here is how.

$ sudo vi /etc/security/limits.conf
> userId soft nofile 4096
> userId hard nofile 4096

This will increase the max-number of file descriptors to 4096. I haven't tried any number greater than this, but this should be enough for most programs.

One interesting thing. Although Mac OSX has a smaller limit of file descriptors, I never experienced this exception in OSX. Is this because OSX's JVM handles file descriptors better than Ubuntu's one?