Is there a way to find out the time since the JVM started?
Of course, other than starting a timer somewhere near the beginning of main
, because in my scenario I am writing library code and the requirement that something be called immediately after startup is a too burdensome.
In short, to get the JVM Start Time-Date you should: Get the JVM's thread system bean, that is the RuntimeMXBean, using the getRuntimeMXBean() API method of ManagementFactory. Use getStartTime() API method to get the start time of the Java virtual machine in milliseconds.
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.
2.1 Possible Causes for Slow JVM StartupThe application might be waiting to import files. A large number of methods might have to be compiled. There might be a problem in code optimization (especially on single-CPU machines). The problem might be caused by the Java application and not the JVM.
Use this snippet:
long jvmUpTime = ManagementFactory.getRuntimeMXBean().getUptime();
or:
long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
This is the correct way of retrieving JVM up-time.
For more info see RuntimeMXBean
You can get the start time of the JVM in the following code:
import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; ... RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); long uptimeInMillis = runtimeMXBean.getUptime();
See more are https://docs.oracle.com/javase/6/docs/api/java/lang/management/RuntimeMXBean.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With