线上问题解决--Socket/File : too many open files(打开的文件过多)

叙述

错误信息

错误: Socket/File : too many open files(打开的文件过多)

从字面意思上看就是说程序打开的文件数过多,不过这里的files不单是文件的意思,也包括打开的通讯链接(比如socket),正在监听的端口等等,所以有时候也可以叫做句柄(handle),这个错误通常也可以叫做句柄数超出系统限制。

背景

  引起的原因就是进程在某个时刻打开了超过系统限制的文件数量以及通讯链接数,通过命令ulimit -a可以查看当前系统设置的最大句柄数是多少:
在这里插入图片描述

查看当前系统打开的文件数量

代码如下:
lsof | wc -l 
watch "lsof | wc -l" 

查看某一进程的打开文件数量

代码如下:
lsof -p pid | wc -l 
lsof -p 1234 | wc -l 

解决方案

1.增大允许打开的文件数——命令方式

1.临时(重启后失效): 
ulimit -n 4096(非root用户限制到4096)    


2.永久生效(需要重启)
vim /etc/security/limits.conf  
#在最后加入  
* soft nofile 4096  
* hard nofile 4096

2.检查程序问题

  如果你对你的程序有一定的解的话,应该对程序打开文件数(链接数)上限有一定的估算,如果感觉数字异常,请使用第一步的lsof -p 进程id > openfiles.log命令,获得当前占用句柄的全部详情进行分析

1)打开的这些文件是不是都是必要的?
2)定位到打开这些文件的代码
3)是否程序操作了文件写入,但是没有进行正常关闭
4)是否程序进行了通讯,但是没有正常关闭(也就是没有超时结束的机制)–(eg:post.releaseConnection()关闭连接)

  代码中只用post.releaseConnection()关闭连接,经过再网上查找资料,说这种关闭并没有真正关闭连接,而是将该连接提交给 MultiThreadedHttpConnectionManager,等待复用。
Close_wate需要延迟几秒钟才能关闭连接,而每个Socket连接都需要等待几秒钟,压力过大时,开启的Socket连接超过了系统所能承受的最大连接数(ulimit -u 1024),所以抛出Too many open files异常。


小结

在写代码的时候规范,规范,规范。

感谢您的阅读~~

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
The error "java socket: too many open files" typically occurs when your Java program reaches the maximum limit of concurrently open files that can be handled by the operating system. This limit is usually determined by the ulimit value set on your system. To resolve this issue, you can take the following steps: 1. Check the current ulimit value by running the command: `ulimit -n`. This will display the maximum number of open files allowed per process. 2. If the current value is too low, you can temporarily increase it by running: `ulimit -n <new_value>`, where `<new_value>` is the desired higher limit. Please note that this change is temporary and will be valid for the current session only. 3. Alternatively, you can permanently increase the ulimit value by modifying the system configuration file. The location of this file varies depending on the Linux distribution you are using. For example, in Ubuntu, you can edit the `/etc/security/limits.conf` file and add a line like this: ``` * hard nofile <new_value> ``` 4. If you're creating multiple socket connections, make sure you close each socket after you're done using it. Failing to close sockets can result in resource leaks and eventually lead to reaching the maximum open file limit. 5. Consider optimizing your code to reuse existing socket connections instead of creating new ones for each request. This can help reduce the number of open files and improve performance. By following these steps, you should be able to resolve the "java socket: too many open files" error.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hi-Sunshine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值