造成这个异常有两类原因:
1.Linux默认的socket描述符数量为1024
抛出这类错误的原因是Linux默认的socket open file的数量为1024,当压力测试达到这个阈值时就会抛出这类错误,下面的文章有详细的解释:
Issue:
How do I set the maximum number of files allowed to be open on a
system
Resolution:
The current setting for maximum number of open files can be viewed
with the command:
ulimit
-n
This number indicates the maximum number of files normal users
(i.e. non-root) can have open in a single session. Note that for
the root user, ulimit -n will sometimes output 1024 even after
following the procedure to increase the maximum number of open
files. This won't effect root's ability to open large numbers of
files, as only normal users are bound by this value.
To increase the maximum number of open files beyond the default
of 1024, two changes to the system may be necessary. In these
examples, we will increase the maximum number of open files to the
arbitrary value of 2048. All changes need to be made by the root
user and users will need to log out and log back in before the
changes will take effect.
1. Configure the system to
accept the desired value for maximum number of open files Check the
value in /proc/sys/fs/file-max to see if it is larger than the
value needed for the maximum number of open files:
# cat /proc/sys/fs/file-max
If the value isn't large enough, echo an appropriate number into
the variable and add the change to /etc/sysctl.conf to make it
persistent across reboots. If the number is already larger than the
value you wish to use, skip to step 2.
# echo 2048 > /proc/sys/fs/file-max
and edit /etc/sysctl.conf to include the line:
fs.file-max = 2048
2. Set the value for
maximum number of open files In the file /etc/security/limits.conf,
below the commented line that reads
#<domain>
<type>
<item>
<value>
add this line:
* - nofile 2048
This line sets the default number of open file descriptors for
every user on the system to 2048. Note that the "nofile" item has
two possible limit values under the
<type> header: hard and soft. Both
types of limits must be set before the change in the maximum number
of open files will take effect. By using the "-" character, both
hard and soft limits are set simultaneously.
The hard limit represents the maximum value a soft limit may have
and the soft limit represents the limit being actively enforced on
the system at that time. Hard limits can be lowered by normal
users, but not raised and soft limits cannot be set higher than
hard limits. Only root may raise hard limits.
When increasing file limit descriptors, you may want to simply
double the value. For example, if you need to increase the default
value of 1024, increase the value to 2048 first. If you need to
increase it again, try 4096, etc.
==================================================================
1。ulimit -a 查看系统目前资源限制的设定。
[root@test security]# ulimit -a
core file
size
(blocks, -c) 0
data seg
size
(kbytes, -d) unlimited
file
size
(blocks, -f) unlimited
max locked
memory
(kbytes, -l) unlimited
max memory
size
(kbytes, -m) unlimited
open
files
(-n) 1024
pipe
size
(512 bytes, -p) 8
stack
size
(kbytes, -s) 8192
cpu
time
(seconds, -t) unlimited
max user
processes
(-u) 7168
virtual
memory
(kbytes, -v) unlimited
[root@test security]#
通过以上命令,我们可以看到open files 的最大数为1024
那么我们可以通过一下命令修改该参数的最大值
2. ulimit -n 4096
[root@test security]# ulimit -n 4096
[root@test security]# ulimit -a
core file
size
(blocks, -c) 0
data seg
size
(kbytes, -d) unlimited
file
size
(blocks, -f) unlimited
max locked
memory
(kbytes, -l) unlimited
max memory
size
(kbytes, -m) unlimited
open
files
(-n) 4096
pipe
size
(512 bytes, -p) 8
stack
size
(kbytes, -s) 8192
cpu
time
(seconds, -t) unlimited
max user
processes
(-u) 7168
virtual
memory
(kbytes, -v) unlimited
这样我们就修改了系统在同一时间打开文件资源的最大数,基本解决以上问题。
另外我们可以通过lsof
-p [进程的
pid]来查看系统当前打开的文件资源,可以了解不同时期系统的文件资源的使用情况,可根据情况进行系统资源的配置。
===================================================================
从这里学到的linux的命令
查看进程所占的文件描述符 lsof -p 390 | wc -l
查看系统的各种资源限制或者更改这个限制
ulimit -a
2.HttpClient建立Socket时
,post.releaseConnection()并没有真正关闭连接,而是将该连接提交给
MultiThreadedHttpConnectionManager,等待复用.
可以设置post方法的header,增加
post.addRequestHeader( "Connection",
"close");
client.getParams().setBooleanParameter(
"http.protocol.expect-continue"
,
false
);
分享到:
相关推荐
JAVA.NET.SOCKETEXCEPTION TOO MANY OPEN FILES解决方法,三种方案,临时,永久,根治
java.net.SocketException: Unrecognized Windows Sockets error: 0: no Inet4Address associated with interface 本文提供了在Win7或Vista系统下解决这个异常信息的办法。
NULL 博文链接:https://zhoudan241.iteye.com/blog/1432014
java.net.SocketException Connection reset 解决方法
最近纠结致死的一个java报错java.net.SocketException: Connection reset 终于得到解决
接口调用报错:java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class com.ibm.websphere.ssl.protocol.SSLSocketFactory
转:java.net.SocketException: Too many open files解决方法最近随着网站访问量的提高把web服务器移到linux下了,在移服务器的第二天,tomcat频繁的报 java.net.SocketException: Too many open files错误,错误...
Linux下运行tomcat或者was出现java.net.SocketException: 打开的文件过多错误 tomcat报“too many open files”的错误,解决办法如下:
Google I/O 2013推荐Android 网络通信框架Volley 下载下来后然后编译为jar包,再在自己的工程里import进来。
socket 文件同步
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站...
System.Net.WebException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集 ---> System.Net.Sockets.SocketException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集 ...
import java.net.SocketException; public class UDPServer { public static void main(String[] args) { try { byte [] buffer = new byte[1024]; DatagramSocket socket = new DatagramSocket(4444); ...
有时会抛出 java.net.SocketException: Invalid argument) Java 行家 运行upload.rb 配置 将 aws 凭据设置为 AwsCredentials.properties 具有标记列和别名列的 csv 文件。 令牌列应命名为“device_token”或...
34. import java.net.SocketException; 35. import java.net.UnknownHostException; 36. import java.util.Date; 37. import java.util.Scanner; 38. import java.util.Stack; 39. import java.util.prefs....
今天在测试邮件功能的时候,发现明明在本地发邮件功能可以正常使用的,但是放到了阿里云服务器上,就发送失败。报错信息如下:System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets....
syslog4j Syslog4j提供了BSD Syslog协议(RFC 3164)和“结构化syslog”协议草案(RFC Draft)的客户端和服务器实现。 警告 这是使用的重新打包的fork,因为原始软件包没有发布到Maven Central的最新版本。...
注意:由于压缩包大于15M,所以分两部分上传,这是第一部分!下载SSH2-1后,再解压这两个文件后就可用了。
Caused by: java.net.ConnectException: Connection refused at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:574) at ...
1.解决服务器报错:java.net.SocketException: No buffer space available (maximum connections reached?): JVM_Bind 2.解决Windows Server 2003/2008 R2系统BUG导致windows缓冲区已满(no buffer space),Windows...