Get the JVM uptime from RuntimeMXBean in Java

Java 8Object Oriented ProgrammingProgramming

RuntimeMXBean in the management interface for the runtime system of the Java virtual machine −

RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();

Let us get the JVM uptime using the getUptime() method −

runtimeMX.getUptime()

The following is an example −

Example

 Live Demo

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Date;
public class Demo {
   public static void main(String args[]) throws Exception {
      RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();
      System.out.println("JVM Uptime = "+runtimeMX.getUptime() + " ms");
   }
}

Output

JVM Uptime = 81 ms

Let us run the code again, to get the following output −

JVM Uptime = 78 ms
raja
Published on 26-Dec-2018 11:19:50
Advertisements