Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8143378

InetAddress.getLocalHost().getHostName() takes 5 seconds

XMLWordPrintable

    • x86
    • os_x

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac OSX 10.11 (El Capitan)

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      HostName: Macbook.local
      LocalHostName: Macbook-3

      Default network configuration in /etc/hosts and /etc/resolve.conf

      A DESCRIPTION OF THE PROBLEM :
      Calling InetAddress.getLocalHost().getHostName(); takes about 5 seconds on JDK 8 update 66 (anything above update 51) and less than a millisecond on JDK update 51.

      On OSX HostName and LocalHostName are set via terminal with help of "scutil --set <pref> <value>" command. Due to possibly Bonjour issues OSX sometimes can decide that your LocalHostName is taken and append a number to it.

      When short version of HostName does not match LocalHostName value, JDK 8 update 66 has a performance regression when calling "InetAddress.getLocalHost().getHostName()".

      Upon manually changing LocalHostName to be identical to short version of HostName, everything performs as expected.

      It would also seem that if HostName is not set or does not contain .local suffix, LocalHostName value does not matter and the bug does not appear.

      REGRESSION. Last worked in version 8u51

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.8.0_51"
      Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
      Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Step 1: In OSX Terminal pre-configure the system by running:
       $ sudo scutil --set HostName MacBook.local
       $ sudo scutil --set LocalHostName MacBook

      Step 2: Run the sample code to validate timings on JDK 8u51 and 8u66.

      Step 3: In OSX Terminal simulate LocalHostName change by running:
       $ sudo scutil --set LocalHostName MacBook-3

      Step 4: Run the sample code to validate timings on JDK 8u51 and 8u66.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Timings for runs in Step 2 and Step 4 versions are very similar for both JDK 8u51 and 8u66.
      ACTUAL -
      Runs in Step 2: similar run times for JDK 8u51 and 8u66
      Runs in Step 4 times differ greatly for JDK 8u51 and 8u66 - with JDK 8u66 suddenly taking approximately 5 seconds per call to "InetAddress.getLocalHost().getHostName()"

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.InetAddress;
      import java.net.UnknownHostException;

      public class NameLookUP {

          public static void main(String[] args) {

              String version = System.getProperty("java.version");
              System.out.println(version);

              Long start = System.currentTimeMillis();

              for (int x=1; x<=100; x++) {
                  try {

                      InetAddress.getLocalHost().getHostName();
                      Long currentElapsed = System.currentTimeMillis() - start;
                      System.out.println("Iteration "+String.valueOf(x)+" current elapsed time "+String.valueOf(currentElapsed));

                  } catch (UnknownHostException e) {
                      e.printStackTrace();
                  }
              }

              Long finish = System.currentTimeMillis();
              Long elapsed = finish - start;

              System.out.println("Total test elapsed time :"+elapsed.toString());
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Manually changing LocalHostName to match short version of HostName setting may be considered a workaround, but I can't consider it to be a clean work-around.

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: