United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 5092063 Extremely slow socket creation using new Socket("ip-address", port)
5092063 : Extremely slow socket creation using new Socket("ip-address", port)

Details
Type:
Enhancement
Submit Date:
2004-08-25
Status:
Resolved
Updated Date:
2005-05-13
Project Name:
JDK
Resolved Date:
2005-05-13
Component:
core-libs
OS:
windows_nt,linux,windows_xp,windows_2000
Sub-Component:
java.net
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
1.4.2,5.0,5.0u5
Fixed Versions:
6

Related Reports
Backport:
Duplicate:
Relates:
Relates:

Sub Tasks

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
                                     
2005-02-15
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
                                     
2005-02-15



Hardware and Software, Engineered to Work Together