追捕 java.net.SocketException: 没有可用的缓冲区空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1226155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hunt down java.net.SocketException: No buffer space available
提问by pnemec
Hi I have very ugly problem with: java.net.SocketException: No buffer space available (maximum connections reached?) It is client-server app. Client is Windows XP SP2 32b, with two net cards core duo. Java 1.6. u7. Application have couple server socket open for local communication and couple of client socket for rmi to jboss server.
嗨,我有一个非常难看的问题:java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?)它是客户端-服务器应用程序。客户端是Windows XP SP2 32b,两块网卡核心。爪哇 1.6。u7. 应用程序为本地通信打开了一对服务器套接字,以及用于 rmi 到 jboss 服务器的一对客户端套接字。
After couple of hours/days! i am unable to open any new client socket to do communication to server. Server sockets still works.
几个小时/天后!我无法打开任何新的客户端套接字与服务器进行通信。服务器套接字仍然有效。
Windows netstat shows something from 130 to 150 connection. When manually trying I exhausted buffer after ~3500 connections!
Windows netstat 显示从 130 到 150 的连接。手动尝试时,我在大约 3500 个连接后耗尽了缓冲区!
I tried:
我试过:
重新启动 java 后,我就可以打开新连接。
Whole Exception:
整个例外:
cause:javax.naming.CommunicationException: Failed to connect to server IP:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server IP:1099 [Roo t exception is java.net.SocketException: No buffer space available (maximum connections reached?): JVM_Bind]] 2009-08-03 09:13:18,968 DEBUG [Thread-9] - stack trace: 2009-08-03 09:13:18,968 DEBUG [Thread-9] - org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1562) 2009-08-03 09:13:18,968 DEBUG [Thread-9] - org.jnp.interfaces.NamingContext.lookup(NamingContext.java:634) 2009-08-03 09:13:18,968 DEBUG [Thread-9] - org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627) 2009-08-03 09:13:18,968 DEBUG [Thread-9] - javax.naming.InitialContext.lookup(Unknown Source)
--edited
We finally hit the problem with malfunctioning snmp server. I wrote my notes in comment bellow. Thanks for help.
--edited
我们终于解决了 snmp 服务器故障的问题。我在下面的评论中写了我的笔记。感谢帮助。
采纳答案by pnemec
What we tried (and successfully) kill the problem.
JAVA
- check again every socket we used, register them in some special class if needed
- provide SocketFactory and ServerSocketFactory for every class which open socket itself (for example jboss Connectors)
- check opened files, close them in finally
- URL opens connection too, but if you ask for stream after that, connection is closed together with stream (thanks Stephen).
OS
- use different java (1.5, 1.6, 1.7)
- install new drivers
- use netstat and monitor traffic on background (using scripts, yes win xp can do the scripts pretty nicely). Use advanced packet sniffers (wire shark?) if needed.
- win xp have limit for concurrent connections, check them (google) too
- check again and again for virus and mallware (even on private network!)
我们尝试(并成功)解决了问题。JAVA - 再次检查我们使用的每个套接字,如果需要,将它们注册到某个特殊类中
- 为每个打开套接字本身的类(例如 jboss 连接器)提供 SocketFactory 和 ServerSocketFactory
- 检查打开的文件,最后关闭它们
- URL 也打开连接,但是如果您在那之后要求流,连接将与流一起关闭(感谢斯蒂芬)。
操作系统
- 使用不同的 java(1.5、1.6、1.7)
- 安装新驱动程序
- 使用 netstat 并在后台监控流量(使用脚本,是的,win xp 可以很好地执行脚本)。如果需要,请使用高级数据包嗅探器(线鲨?)。
- win xp 对并发连接有限制,也请检查它们(谷歌)
- 一次又一次地检查病毒和恶意软件(即使是在专用网络上!)
回答by Stephen C
It certainly sounds like you are leaking Sockets somehow in your app.
听起来您似乎在以某种方式在您的应用程序中泄漏了 Sockets。
- Check that your code always closes
the Sockets it opens ... even in the
event of some exception; i.e. do the
close in a
finally
block. - If your code uses URL connections, make sure that they get disconnected.
- I'm not an expert, but should your code close its InitialContext object?
- 检查您的代码是否总是关闭它打开的 Sockets ......即使发生了一些异常;即在一个
finally
块中关闭。 - 如果您的代码使用 URL 连接,请确保它们断开连接。
- 我不是专家,但是您的代码应该关闭它的 InitialContext 对象吗?
回答by Mike
After reading the advice offered in this link! I was able to determine that I was using isDisplayed() way too often in too short of a time. Therefore, I placed a 5 millisecond wait between calls to isDisplayed. This fixed my Socket Exception issue.
阅读此链接中提供的建议后!我能够确定我在太短的时间内使用 isDisplayed() 的方式太频繁了。因此,我在调用 isDisplayed 之间放置了 5 毫秒的等待时间。这修复了我的套接字异常问题。
for (final WebElement person: persons){
if (person.isDisplayed()){
dosomething;
sleep 5 milliseconds
}
}
As stated in the link you should insert a try catch just in case this wait is not long enough.
如链接中所述,您应该插入一个 try catch 以防等待时间不够长。