I'm having a problem using URLConnections that I'm hoping someone can shed some light on. The basic problem is: if a client creates numerous URLConnections very rapidly, eventually it stops being able to open any more connections without waiting a few minutes for a resource (client ports?) to be released.
I've simplified the problem down to a relatively simple test case:
I run Tomcat, and drop a simple HTML file in the root. I then run a small Java program that repeatedly opens a URLConnection to the file, reads it, and releases the connection. I find that on the 3963rd attempt, the connection fails. After that, any more attempts fail for approximately 2 minutes. Then it works again and fails on the 3963rd attempt again. If I run "netstat" in a command prompt right after running the test program, a huge list of connections is displayed. This list seems to clear about the same time the program starts to work again.
I wrote an identical program that uses sockets to connect instead of URLConnections, and it can run indefinitely without ever getting errors. However if I run it right after the URLConnection test has failed, it fails as well.
What I've concluded from these tests is that the URLConnection class does not release some resource (I believe this resource is "client ports" allocated by Windows) for about two minutes. Even calling HttpURLConnection.disconnect() does not release this resource. Sockets seem to use the same resource, but release this resource immediately when close() is called.
Does anyone know of a way to get the URLConnection class to release this resource immediately? Or any workaround other than writing all my HTTP communication from scratch using sockets?
Information about the platform I'm using:
- Windows XP
- Java 1.3.1_08 (also tested with 1.4.2_02 - same behavior)
Here is my test program that uses URLConnections:
And here is my equivalent test program that uses sockets:
Thanks in advance for any help..
[ May 27, 2004: Message edited by: Will Ross ]
I saw a similar discussion here indicating that this is just a Windows issue.
Originally posted by Stefan Wagner:
I tested your code with:
OS: Linux 2.6.5
Webserver: xitami v2.5b5
java: 1.5b and java: 1.4.2
runs fine with 7000 requests for both javas,
and - only tested with 1.5: for 70.000 requests - no pause involved, and memory monitoring showed constant value.