# java.net.SocketException：打开的文件过多

- **ID:** `java/socketexception-too-many-open-files`
- **领域:** java
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

JVM 进程已耗尽操作系统设置的文件描述符限制，通常由应用程序中未关闭的套接字、文件流或其他 I/O 资源引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |
| Linux kernel 5.x | active | — | — |

## 解决方案

1. ```
   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.
   ```
2. ```
   Monitor the open file count using lsof on Linux to identify which resources are leaking. Use that information to fix the specific leak.
   ```
3. ```
   Temporarily increase the file descriptor limit for the JVM process using ulimit or systemd configuration, combined with a fix for the leak.
   ```

## 无效尝试

- **** — Increasing the file descriptor limit on the OS without fixing the leak only delays the crash; the leak continues. (70% 失败率)
- **** — Restarting the application temporarily frees descriptors but does not fix the root cause of resource leaks. (85% 失败率)
- **** — Adding more memory does not affect file descriptor limits; the issue is about OS limits, not heap. (95% 失败率)
