JDK-5092063 : Extremely slow socket creation using new Socket("ip-address", port)
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.4.2,5.0,5.0u5
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    linux,windows_nt,windows_2000,windows_xp linux,windows_nt,windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2004-08-25
  • Updated: 2005-05-13
  • Resolved: 2005-05-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u6Fixed 6 betaFixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Name: gm110360			Date: 08/25/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows XP professional SP1

EXTRA RELEVANT SYSTEM CONFIGURATION :
simple LAN network, 1router, 1 PC, router contains DNS,  DHCP, etc. router connected to Internet

A DESCRIPTION OF THE PROBLEM :
The creation of a socket using new Socket("ip-address", port) seems to be extremely slow. On my machine it takes up to 5 seconds to create the socket. Multiple sockets (in sequence, not parallel) take approximately the same amount of time.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the provided sample code on java1.5.0 on Windows XP Professional SP1.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expected the time to open sockets to be very small (<10ms).
ACTUAL -
Output of the sample code in Java1.5.0:
Time to open socket (ms): 4515
Time to open socket (ms): 4500
Time to open socket (ms): 4500
Time to open socket (ms): 4500
Time to open socket (ms): 4500

Output of the same sample code in Java1.4.2_05:
Time to open socket (ms): 31
Time to open socket (ms): 0
Time to open socket (ms): 0
Time to open socket (ms): 0
Time to open socket (ms): 16


ERROR MESSAGES/STACK TRACES THAT OCCUR :
no errors, just slow responses

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
for(int i=0;i<5;i++)
{
    long time_start = System.currentTimeMillis();
    Socket s = new Socket("192.168.1.1",80);
    long time_end = System.currentTimeMillis();
    System.out.println("Time to open socket (ms): "+(time_end-time_start));
    s.close();
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
A profiler showed that the problem may not lie in the socket implementation but in the InetAddress implementation because most of the waiting time was spent there.

A workaround was by creating a InetAddress myself, cache it, and pass that to the constructor of the sockets. This resulted in the behaviour that the first socket took about 4.5 seconds to be created, the others were much faster. Perhaps the bug has something to do with DNS resolving? Note that I do not pass a DNS name to the Socket constructor but a String representation of a IPv4 address. It is not a bug of the router, as I have tried multiple routers (from different vendors).

I use the sockets to do some HTTP requests. Using HttpUrlConnection to get the HTTP contents made my program much much faster. This makes me believe that the error is not in my OS, but in the implementation of the java Socket classes. There is something in the construction of a socket that is not in the HttpUrlConnection that makes socket creation very slow.
(Incident Review ID: 297298) 
======================================================================

Comments
EVALUATION Finally was able to reproduce this. There is a call to InetSocketAddress.getHostName() in the ProxySelector code. This will trigger a reverse lookup when the hostname is not already known. If DNS is not configured correctly on the machine, this will generate a long timeout. Will fix as soon as possible. ###@###.### 2005-2-15 16:50:30 GMT
15-02-2005

WORK AROUND Long timeouts are due to DNS being badly configured. Customers experiencing this issue should try to fix the DNS configuration on the machines (i.e. make sure the DNS servers do exist). ###@###.### 2005-2-15 16:50:30 GMT
15-02-2005