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

RuntimeMXBean.getUptime() returns negative values

XMLWordPrintable

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

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows XP [Build 5.1.2600]

        A DESCRIPTION OF THE PROBLEM :
        RuntimeMXBean.getUptime() incorrectly calculates uptime of JVM. The method assumes that uptime equals to (current_time - JVM_start_start). It's wrong because system time can be changed by some external process. Of couse, uptime (due to its semantics) cannot be less than 0. RuntimeMXBean.getUptime() should use OS timers to get elapsed time of JVM process.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Compile and run the attached sample

        2. Change system time (for example with help of standard Windows utility) - one day before today

        3. Take a look at program's output.


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        I should see growing sequence of non negative values
        ACTUAL -
        Something like this:
        uptime = 3084
        uptime = 3585
        uptime = 4086
        uptime = 4587
        uptime = -604795913
        uptime = -604795412
        uptime = -604794912


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.lang.management.ManagementFactory;
        import java.lang.management.RuntimeMXBean;

        /**
         * @author Vladimir Kondratyev
         */
        public class test {
          public static void main(String[] args) throws Exception{
            final Thread thread = new Thread(){
              public void run() {
                while (true) {
                  final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
                  final long uptime = runtimeMXBean.getUptime();
                  System.out.println("uptime = " + uptime);
                  try {
                    Thread.sleep(500);
                  }
                  catch (final InterruptedException ignored) { }
                }
              }
            };
            thread.run();
          }
        }
        ---------- END SOURCE ----------

              jbachorik Jaroslav Bachorík
              tbell Tim Bell
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: