解决创建socket时Too many open files问题

提高文件描述符限制
本文介绍了如何通过修改系统配置文件来增加Linux系统中文件描述符的数量,并提供了针对程序编译时遇到的文件大小限制问题的解决方案。
转载:http://blog.chinaunix.net/uid-10666692-id-1759617.html

首先按照网络上的指导操作修改如下内容(需要重启):
ulimit -n 65535(可以写入启动文件/etc/rc.d/rc.local中)
/etc/sysctl.conf : fs.file-max = 65535
/etc/security/limits.conf : * - nofile 65535
如果这样还是不能解决问题,那可能是程序编译时有filesize的限制。
比如我的程序运行时有如下告警:
Warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE = 1024
因此需要修改/usr/include/bits/typesizes.h头文件中的__FD_SET_SIZE宏志,该值默认为1024,修改成65535重新编译即可。
 
另外可以利用如下命令查看进程占用文件数:
lsof -p 进程号 |wc -l
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.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值