Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time since JVM started

Tags:

java

time

jvm

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.

like image 214
flybywire Avatar asked May 03 '09 19:05

flybywire


People also ask

How to Check JVM Start time?

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.

How to Check JVM Uptime?

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.

Why does Java take so long to start?

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.


2 Answers

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

like image 180
Yuval Adam Avatar answered Oct 05 '22 11:10

Yuval Adam


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.

like image 36
David Rabinowitz Avatar answered Oct 05 '22 12:10

David Rabinowitz