Java IOException“打开的文件太多" [英] Java IOException "Too many open files"

查看:158
本文介绍了Java IOException“打开的文件太多"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用多个文件执行一些文件I/O(写入19个文件,确实如此).写了几百遍之后,我得到了Java IOException:Too many open files.但是实际上我一次只能打开几个文件.这里有什么问题?我可以验证写入是否成功.

I doing some file I/O with multiple files (writing to 19 files, it so happens). After writing to them a few hundred times I get the Java IOException: Too many open files. But I actually have only a few files opened at once. What is the problem here? I can verify that the writes were successful.

推荐答案

在Linux和其他UNIX/类似UNIX的平台上,操作系统对进程在任何给定时间可能具有的打开文件描述符的数量进行了限制.在过去,此限制以前是硬连接的 1 ,并且相对较小.如今,它要大得多(几百/几千),并且受软"每进程可配置资源的限制. (查找ulimit shell内置...)

On Linux and other UNIX / UNIX-like platforms, the OS places a limit on the number of open file descriptors that a process may have at any given time. In the old days, this limit used to be hardwired1, and relatively small. These days it is much larger (hundreds / thousands), and subject to a "soft" per-process configurable resource limit. (Look up the ulimit shell builtin ...)

您的Java应用程序必须超出每个进程的文件描述符限制.

Your Java application must be exceeding the per-process file descriptor limit.

您说打开了19个文件,几百次之后,您得到一个IOException消息,说打开的文件太多".现在,仅当请求新的文件描述符时,才可能发生此特殊异常.也就是说,当您打开文件(或管道或套接字)时.您可以通过打印IOException的堆栈跟踪信息来验证.

You say that you have 19 files open, and that after a few hundred times you get an IOException saying "too many files open". Now this particular exception can ONLY happen when a new file descriptor is requested; i.e. when you are opening a file (or a pipe or a socket). You can verify this by printing the stacktrace for the IOException.

除非您的应用程序运行时资源有限(似乎不太可能),否则必须重复打开文件/套接字/管道,但无法关闭它们.找出发生这种情况的原因,您应该能够弄清楚该怎么办.

Unless your application is being run with a small resource limit (which seems unlikely), it follows that it must be repeatedly opening files / sockets / pipes, and failing to close them. Find out why that is happening and you should be able to figure out what to do about it.

仅供参考,以下模式是一种写入文件的安全方法,可以保证不会泄漏文件描述符.

FYI, the following pattern is a safe way to write to files that is guaranteed not to leak file descriptors.

Writer w = new FileWriter(...);
try {
    // write stuff to the file
} finally {
    try {
        w.close();
    } catch (IOException ex) {
        // Log error writing file and bail out.
    }
}


1-硬接线,如编译到内核中一样.更改可用fd插槽的数量需要重新编译...并可能导致可用于其他用途的内存减少.在Unix通常在16位计算机上运行的时代,这些事情确实很重要.

更新

Java 7方式更简洁:

The Java 7 way is more concise:

try (Writer w = new FileWriter(...)) {
    // write stuff to the file
} // the `w` resource is automatically closed 


更新2

显然,在尝试运行外部程序时,您还会遇到打开的文件过多"的问题.基本原因如上所述.但是,在exec(...)中遇到此问题的原因是JVM试图创建管道"文件描述符,该描述符将连接到外部应用程序的标准输入/输出/错误.

Apparently you can also encounter a "too many files open" while attempting to run an external program. The basic cause is as described above. However, the reason that you encounter this in exec(...) is that the JVM is attempting to create "pipe" file descriptors that will be connected to the external application's standard input / output / error.

这篇关于Java IOException“打开的文件太多"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆