time since JVM started

后端 未结 5 1075
眼角桃花
眼角桃花 2020-12-04 19:12

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 sc

相关标签:
5条回答
  • 2020-12-04 19:21

    Starting from Java 5, you can use JMX to find this out. Check "Using the platform MBeanserver" to find out more details. The bean you're looking for is the bean called "java.lang:type=Runtime". The attribute StartTime gives you the time the JVM was started and the Uptime attribute tells you the uptime of the JVM.

    You can get a reference to the Runtime bean by executing this code: ManagementFactory.getRuntimeMXBean();

    0 讨论(0)
  • 2020-12-04 19:32

    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.

    0 讨论(0)
  • 2020-12-04 19:33

    Perhaps worth mentioning that if you don't want to write any code run up jconsole from the java bin directory and click VM Summary and see the Uptime and Process CPU time values.

    0 讨论(0)
  • 2020-12-04 19:47

    if your jvm program running in linux, you can view the startTime use ps

    ps -p <pid> -o stime,etime 
    
    0 讨论(0)
  • 2020-12-04 19:48

    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 http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/RuntimeMXBean.html

    0 讨论(0)
提交回复
热议问题