`

压力测试时抛出java.net.SocketException: Too many open files

阅读更多

造成这个异常有两类原因:

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: Too many open files 错误。这种错误的出现会导致服务器崩溃,影响应用程序的稳定性和可靠性。 解决方法一:临时解决方案 使用命令 ...

    在Win7系统下抛出java.net.SocketException的解决办法

    ### 在Win7系统下抛出java.net.SocketException的解决办法 #### 问题背景及原因分析 在使用MyEclipse开发工具结合JBoss服务器进行项目开发的过程中,遇到一个常见但令人头疼的问题:当启动JBoss服务器时,控制台...

    java.net.SocketException: No route to host 异常原因

    NULL 博文链接:https://zhoudan241.iteye.com/blog/1432014

    java.net.SocketException Connection reset 解决方法

    "java.net.SocketException Connection reset 解决方法" 在 Java 编程中,SocketException 是一种常见的异常,特别是在网络编程中。Conexion reset by peer 是一种特殊的 SocketException,它发生在客户端和服务器...

    java.net.SocketException: Connection reset 解决方法

    Java中的`java.net.SocketException: Connection reset`是一个常见的网络编程错误,通常表示在TCP/IP通信过程中,连接突然中断。这个异常可能在客户端或服务器端发生,通常与数据传输的异常中断有关。 首先,我们来...

    Linux下打开的文件过多错误

    Linux下运行tomcat或者was出现java.net.SocketException: 打开的文件过多错误 tomcat报“too many open files”的错误,解决办法如下:

    http接口调用报错.rar

    接口调用报错:java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class com.ibm.websphere.ssl.protocol.SSLSocketFactory

    Java Socket常见异常处理

    4. **java.net.SocketException: (Connection reset 或 Connect reset by peer: Socket write error)** 这两种异常都表示连接已断开。可能的原因是一端关闭了Socket,而另一端仍在尝试读写。处理这类异常需要检测到...

    connection reset解决方案(亲测可用).md

    connection reset解决方案(亲测可用).md

    fastdfs使用java

    总的来说,使用Java与FastDFS集成,可以构建出强大的文件管理系统,实现高效的文件上传和下载,同时通过数据库存储文件元信息,便于管理和检索。在实际开发中,应结合具体业务需求,优化配置和代码实现,以达到最佳...

    Too many open files 解决方法

    在将Web服务器从Windows环境迁移到Linux环境后,遇到了一个常见的问题:“java.net.SocketException: Too many open files”。这种异常通常发生在高并发环境下,例如网站访问量显著增加时。在Windows环境中长时间...

    Ice-3.7.4.msi for windows版

    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 ...

    java.net.SocketException(解决方案).md

    项目中碰到的,记录一下解决方案

    Google I/O 2013推荐Android 网络通信框架Volley

    当数据请求返回时,Volley会自动将结果存储到缓存中,以便后续请求可以快速从缓存中获取数据,减少了网络延迟。 2. **线程管理**:Volley通过一个线程池来管理网络请求,将网络操作与主线程分离,避免了因网络请求...

    Hive2Hive-master.rar

    在Java中,Socket类和ServerSocket类分别用于客户端和服务端的通信建立。 2. **Hive数据同步需求**: 在大数据环境中,数据的实时同步是非常关键的。例如,当一个Hive集群中的数据发生变化时,可能需要将这些变化...

    Socket长连接异常处理

    java.net.SocketException: Connection reset by peer: Socket write error 该异常可能发生在客户端和服务器端,原因是因为一端的 Socket 被关闭,而另一端继续发送数据。解决方法是确保在关闭连接前完成所有的数据...

    如何解决线程太多导致java socket连接池出现的问题

    3. 错误信息:当Socket连接池出现问题时,可能会出现错误信息,如"java.net.SocketException: No buffer space available (maximum connections reached?): listen failed"。 解决方法 要解决线程太多导致Socket...

    System.SR.dll 无法显示错误信息

    System.Net.WebException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集 ---&gt; System.Net.Sockets.SocketException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集 ...

    C# 服务器发送邮件失败实例分析

    “{“success”:false,”message”:”错误System.Net.Mail.SmtpException: Failure sending mail. —&gt; System.Net.WebException: The remote name could not be resolved: ‘smtp.163.com’ 分析: 邮件发送相关...

Global site tag (gtag.js) - Google Analytics