How to get Uptime Java JVM running Application

This tutorial shows you how to get the uptime (in milliseconds) of your running JVM/java application. This could be used to display in a fancy dashboard for your users or administrators. We can obtain this uptime information using the RuntimeMXBean class. Which is the management interface for the runtime system of the JVM. This interface is also used to get other information about the JVM installed on your computer/server.

Get Uptime JVM

We can use the RuntimeMXBean class to get JVM specific information. We obtain an instance by calling the getRuntimeMXBean() method of the ManagementFactory factory class. Here, we can get the uptime java using the getUptime() method. This returns the uptime of the Java Virtual Machine in milliseconds.

package com.memorynotfound;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetUpTime {

    public static void main(String... args) throws InterruptedException {
        RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();

        System.out.println("Up time: " + rb.getUptime() + " ms");
        Thread.sleep(1000 * 10);
        System.out.println("Up time: " + rb.getUptime() + " ms");
    }
}

Output Uptime Java

Up time: 59 ms
Up time: 10063 ms

References

You may also like...