title: Why do System.nanoTime() and System.currentTimeMillis() drift apart so rapidly?
body: <p>For diagnostic purposes, I want to be able to detect changes in the system time-of-day clock in a long-running server application.  Since <code>System.currentTimeMillis()</code> is based on wall clock time and <code>System.nanoTime()</code> is based on a system timer that is independent(*) of wall clock time, I thought I could use changes in the difference between these values to detect system time changes.</p>
  
  <p>I wrote up a quick test app to see how stable the difference between these values is, and to my surprise the values diverge immediately for me at the level of several milliseconds per second.  A few times I saw much faster divergences.  This is on a Win7 64-bit desktop with Java 6.  I haven't tried this test program below under Linux (or Solaris or MacOS) to see how it performs.  For some runs of this app, the divergence is positive, for some runs it is negative.  It appears to depend on what else the desktop is doing, but it's hard to say.</p>
  
  <pre><code>public class TimeTest {
    private static final int ONE_MILLION  = 1000000;
    private static final int HALF_MILLION =  499999;
  
    public static void main(String[] args) {
      long start = System.nanoTime();
      long base = System.currentTimeMillis() - (start / ONE_MILLION);
  
      while (true) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          // Don't care if we're interrupted
        }
        long now = System.nanoTime();
        long drift = System.currentTimeMillis() - (now / ONE_MILLION) - base;
        long interval = (now - start + HALF_MILLION) / ONE_MILLION;
        System.out.println("Clock drift " + drift + " ms after " + interval
                           + " ms = " + (drift * 1000 / interval) + " ms/s");
      }
    }
  }
  </code></pre>
  
  <p>Inaccuracies with the <code>Thread.sleep()</code> time, as well as interruptions, should be entirely irrelevant to timer drift.</p>
  
  <p>Both of these Java "System" calls are intended for use as a measurement -- one to measure differences in wall clock time and the other to measure absolute intervals, so when the real-time-clock is not being changed, these values should change at very close to the same speed, right?  Is this a bug or a weakness or a failure in Java?  Is there something in the OS or hardware that prevents Java from being more accurate?</p>
  
  <p>I fully expect some drift and jitter(**) between these independent measurements, but I expected well less than a minute per day of drift.  1 msec per second of drift, if monotonic, is almost 90 seconds!  My worst-case observed drift was perhaps ten times that.  Every time I run this program, I see drift on the very first measurement.  So far, I have not run the program for more than about 30 minutes.</p>
  
  <p>I expect to see some small randomness in the values printed, due to jitter, but in almost all runs of the program I see steady increase of the difference, often as much as 3 msec per second of increase and a couple times much more than that.</p>
  
  <p>Does any version of Windows have a mechanism similar to Linux that adjusts the system clock speed to slowly bring the time-of-day clock into sync with the external clock source?  Would such a thing influence both timers, or only the wall-clock timer?  </p>
  
  <p>(*) I understand that on some architectures, <code>System.nanoTime()</code> will of necessity use the same mechanism as <code>System.currentTimeMillis()</code>.  I also believe it's fair to assume that any modern Windows server is not such a hardware architecture.  Is this a bad assumption?</p>
  
  <p>(**) Of course, <code>System.currentTimeMillis()</code> will usually have a much larger jitter than <code>System.nanoTime()</code> since its granularity is not 1 msec on most systems.</p>