Java太多打开的文件 [英] Java Too Many Open Files

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

问题描述

我正在试图写入多个文件,准确地说,19。给他们写了几百次之后,我得到了Java IOException:打开的文件过多。但是,正如我所说,我已经打开了19个文件,并在开始时打开它们。这里有什么问题?我可以验证写入是否成功。

编辑:
我没有使用try-catch-finally块。我有函数抛出异常,而不是。现在我把try-catch-finally放在他们周围,他们似乎做得更好。



大多数人是对的,我打开的文件比我想象的要多。还在追踪事情

重新编辑:
确保所有的文件访问都被try-catch-finally包装,解决了这个问题。在Linux和其他类UNIX / UNIX平台上,操作系统限制了打开的文件描述符的数量一个过程可能在任何给定的时间。在过去,这个限制以前是硬连线的,而且比较小。现在它大得多(数百/数千),并受制于每个进程可配置的软限制。 (查找 ulimit shell内置...)



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

你说你打开了19个文件,并且在几百次之后,你会得到一个IOException,表示打开的文件过多。现在这个特殊的异常只能在请求一个新的文件描述符时才会发生。即当你打开一个文件(或一个管道或一个套接字)的时候。您可以通过打印IOException的堆栈跟踪来验证。



除非您的应用程序运行时资源限制很小(这似乎不太可能) ,那么它必须重复打开文件/套接字/管道,并且不能关闭它们。找出为什么发生这种情况,你应该能够弄清楚该怎么做。



仅供参考,以下模式是写入文件的安全方式保证不泄漏文件描述符。

  Writer w = new FileWriter(...); 
尝试{
//将文件写入文件
} finally {
try {
w.close();
} catch(IOException ex){
//记录错误,写入文件并保存。






1 - 硬连线,如编译进内核一样。改变可用fd插槽的数量需要重新编译...并且可能导致其他内存不足。在Unix通常在16位机器上运行的时代,这些事情是非常重要的。


$ b

更新



Java 7的方式更加简洁:

$ p $ try(Writer w = new FileWriter ...)){
//将文件写入文件
} // w`资源自动关闭






更新2

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

I am trying to write to multiple files, 19 to be exact. After writing to them a few hundred times I get the Java IOException: Too many open files. But, like I said I have exactly 19 files open and I opened them all at the beginning. What is the problem here? I can verify that the writes were successful.

edit: I wasn't using a try-catch-finally block. I had the functions throw an exception instead. Now that I put the try-catch-finally around them they seem to be doing better.

Most of you were right in that I am opening more files than I thought. Still tracking things down. I'll post an update after a bit.

re-edit: ensuring all file access were wrapped with try-catch-finally fixed the problem. Thanks

解决方案

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 ...)

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

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 - Hardwired, as in compiled into the kernel. Changing the number of available fd slots required a recompilation ... and could result in less memory being available for other things. In the days when Unix commonly ran on 16-bit machines, these things really mattered.

UPDATE

The Java 7 way is more concise:

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


UPDATE 2

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太多打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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