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

System.nanoTime() results vary between Java SE 6.0 and 5.0

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 6
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.6.0-beta2"
      Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
      Java HotSpot(TM) 64-Bit Server VM (build 1.6.0-beta2-b86, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux kowloon 2.6.9-34.ELsmp #1 SMP Fri Feb 24 16:56:28 EST 2006 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      System.nanoTime() is returning values which don't agree with what System.currentTimeMillis().

      The bug was found on Linux AS4 on an Opteron processor, running the Java 1.6 beta 2 JVM (64-bit). The problem does not exist if I use the Java 1.5 32-bit JVM on the same machine.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the sample code below, as follows (output from console on the Linux host 'kowloon'):

      kowloon% uname -a
      Linux kowloon 2.6.9-34.ELsmp #1 SMP Fri Feb 24 16:56:28 EST 2006 x86_64 x86_64 x86_64 GNU/Linux

      kowloon% cat Test.java
      import java.lang.*;
      import java.util.*;

      public class Test
      {
          public static void main(String args[])
          {
              System.err.println("nanoTime = " + System.nanoTime());
              System.err.println("milliTime = " + System.currentTimeMillis());
          }
      }

      kowloon% /usr/jdk1.6.0/bin/javac Test.java

      kowloon% /usr/jdk1.6.0/bin/java Test
      nanoTime = 14112906333480828
      milliTime = 1159561880925

      kowloon% /usr/jdk1.5.0_02/bin/javac Test.java

      kowloon% /usr/jdk1.5.0_02/bin/java Test
      nanoTime = 1159561909537191000
      milliTime = 1159561909537

      kowloon% /usr/jdk1.6.0/bin/java -version
      java version "1.6.0-beta2"
      Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
      Java HotSpot(TM) 64-Bit Server VM (build 1.6.0-beta2-b86, mixed mode)

      kowloon% /usr/jdk1.5.0_02/bin/java -version
      java version "1.5.0_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
      Java HotSpot(TM) Server VM (build 1.5.0_02-b09, mixed mode)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      System.nanoTime() should return a value approximately equal to System.currentTimeMillis() * 1000
      ACTUAL -
      Return value did not resemble System.currentTimemillis().

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.*;
      import java.util.*;

      public class Test
      {
          public static void main(String args[])
          {
              System.err.println("nanoTime = " + System.nanoTime());
              System.err.println("milliTime = " + System.currentTimeMillis());
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use System.currentTimeMillis() * 1000 whenever nanoTime is required. This of course causes a great loss in precision.

            martin Martin Buchholz
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: