爬蟲程式運行中出現異常,
java.net.SocketException: No buffer space available (maximum connections reached?): JVM_Bin
爬蟲中使用httpclient,而且是使用了線程池。
搜了下發現,別人也有類似問題,解決辦法如下,照著改了下程式,看看以後運行中是否還會出現這個問題。
from http://9iopen.group.javaeye.com/group/blog/144545
在一個爬蟲程式中遇到了以下異常:
java.net.SocketException No buffer space available (maximum connections reached?): JVM_Bind
我們知道,作業系統有它允許持有的最大檔案控制代碼數,而在網路連接的過程中,每個socket請求都要佔用一個檔案控制代碼資源,如果沒有及時釋放,則可能會耗盡檔案控制代碼資源.
通過檢查代碼,發現在使用HttpClient發送Get請求時沒有釋放資源,並且由於是多線程程式,很容易耗盡資源
改寫後的代碼如下: java 代碼 HttpClient client = new HttpClient(); GetMethod method = null ; try { method = new GetMethod(crawlerURL.getUURI().getEscapedURI()); statusCode = client.executeMethod(method); //... } finally { if ( null != method) method.releaseConnection(); }
在sun的技術論壇中有一個解答是這樣的:
Chances are you are forgetting to close a socket, a database connection, or some other connection that uses sockets internally. See example program below.
The alternative is that your program (or the sum of all programs running on your computer) really needs a lot of connections. In this case you'll need to find out how to increase the amount of socket buffer space that your operating system allocates. I'd start by googling for instructions. Or maybe you could redesign your program so that it doesn't use so much resources.
更多此問題的解答請看這裡:
http://forum.java.sun.com/thread.jspa?threadID=556382