java
resource_error
ai_generated
true
java.net.SocketException:打开的文件过多
java.net.SocketException: Too many open files
ID: java/socketexception-too-many-open-files
85%修复率
85%置信度
1证据数
2023-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Java 8 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
| Linux kernel 5.x | active | — | — | — |
根因分析
JVM 进程已耗尽操作系统设置的文件描述符限制,通常由应用程序中未关闭的套接字、文件流或其他 I/O 资源引起。
English
The JVM process has exhausted the file descriptor limit set by the operating system, typically due to unclosed sockets, file streams, or other I/O resources in the application.
官方文档
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/SocketException.html解决方案
-
Use try-with-resources to ensure all Closeable resources (sockets, streams) are automatically closed. Review code for missing close() calls in finally blocks or using try-with-resources.
-
Monitor the open file count using lsof on Linux to identify which resources are leaking. Use that information to fix the specific leak.
-
Temporarily increase the file descriptor limit for the JVM process using ulimit or systemd configuration, combined with a fix for the leak.
无效尝试
常见但无效的做法:
-
70% 失败
Increasing the file descriptor limit on the OS without fixing the leak only delays the crash; the leak continues.
-
85% 失败
Restarting the application temporarily frees descriptors but does not fix the root cause of resource leaks.
-
95% 失败
Adding more memory does not affect file descriptor limits; the issue is about OS limits, not heap.