JDK-6523160 : RuntimeMXBean.getUptime() returns negative values
  • Type: Bug
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-02-09
  • Updated: 2013-12-05
  • Resolved: 2013-11-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 Other
8 b117Fixed port-stage-ppc-aixFixed
Related Reports
Relates :  
Sub Tasks
JDK-8027294 :  
Description
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 ----------

Comments
The patch is ready. Review has been requested.
07-10-2013

EVALUATION Uptime should never be negative. We should report the uptime using the VM high-resolution timer.
09-03-2007