How to resolve java.net.SocketException: Too many open files

I tested the load for my REST APIs using JMeter.

I get the following error on impact with 1000 concurrent users:

Too many open files. Stacktrace follows: java.net.SocketException: Too many open files at java.net.Socket.createImpl(Socket.java:397) at java.net.Socket.getImpl(Socket.java:460) at java.net.Socket.setSoTimeout(Socket.java:1017) at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:126) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:476) at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441) at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:390) 

My server is trying to get into another REST API to get the data and process it and finally return a JSON response.

How to increase the number of open files in Linux?

Below is the call I am making to another server

 Map getResponse(Map data, String url){ HTTPBuilder httpBuilder = new HTTPBuilder(url); httpBuilder.request(Method.POST, JSON) { headers.'Authorization' = AppConfig.config.appKey; headers.'Content-type' = 'application/json' body = data response.success = { resp, reader -> return reader as Map; } response.failure = { response, reader -> return null } } } 
+9
source share
2 answers

You probably opened the maximum number of open files / sockets. The maximum number of open files or sockets on Linux computers is 1024. by default. You have to change that. You can refer to this java.net.SocketException Too many open files

You can use the query below to check from your terminal to get the maximum number of allowed open files

 ulimit -n 

From here here :

What happens is that the underlying sockets do not close, and ultimately the JVM is confronted with a system limit on the process of opening file descriptors.

The correct solution would be to close the sockets on the right Time (which I guess when or shortly after the server closed its end connection). This seems complicated with HttpURLConnection. This is all very confusing:

  • disnect () just seems to close it immediately - or not; Javadocs are intentionally vague about what it actually does, and especially when it does.

  • close () might be the right choice. The evaluation section of Java Error # 4147525 reads: "... call close () to enter and / or the output stream. This will correctly cause the socket to close when you are not making a keepalive connection and will properly cache and reuse the keepalive connection (and which will time out and close yourself anyway after a while). "

  • But maybe not. Mistake No. 4142971 reads: "Calling closure () methods does not in any way affect whether the underlying HTTP connection is persistent."

If you do not give a clear answer, perhaps the HttpURLConnection objects could be added to the list, and all are disabled immediately at the end of the test run. This still limits the overall trace size, but at least the lost descriptors will not accumulate between runs.

Perhaps the real answer is to drop the HttpURLConnection, and use the Jakarta Commons HTTP client instead. Someone suggested that in connection with another problem (bug #4143518 ).

+5
source

"java.net.SocketException: too many files open" you can see any Java Server application, for example. Tomcat, Weblogic, WebSphere, etc. With frequent connection and disconnection of the client.

You can find out how to solve "java.net.SocketException: Too many files open here

0
source

All Articles