[Solved] Java Error: Too many open files [duplicate]


The most likely explanation is that you have a bug in your Java application that is causing it to leak file open files. The way to avoid the problem is to write your code so that file handles are always closed; e.g.

    // Using the 'try with resource' construct
    try (Reader r = new FileReader(...)) {
        // use the reader
    } catch (IOException ex) {
        // ...
    }

or

    // Using classic try/catch/finally
    Reader r = null;
    try {
        r = new FileReader(...);
        // use the reader
    } catch (IOException ex) {
        // ...
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (IOException ex) {
                // ignore
            }
        }
    }

The chances are that the program worked on one operating system and not another because the limit on the number of open files is different on the two systems. If the Java GC runs, it will close any unreachable file handles that it finds via the respective handle’s finalizer. However, this may not happen in time to prevent the “too many files open” exception.


If your application actually requires a large number of files to be open simultaneously, you will need to lift the limit. This is best done using the ulimit shell built-in, assuming that you are using an UNIX-based operating system.

Note however that there will still be a hard limit that it is not possible to exceed. And I believe that applies for Windows as well.

solved Java Error: Too many open files [duplicate]