time since JVM started

2019-01-16 17:54发布

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.

标签: java time jvm
5条回答
仙女界的扛把子
2楼-- · 2019-01-16 18:26

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

查看更多
戒情不戒烟
3楼-- · 2019-01-16 18:27

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

查看更多
太酷不给撩
4楼-- · 2019-01-16 18:27

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

ps -p <pid> -o stime,etime 
查看更多
Explosion°爆炸
5楼-- · 2019-01-16 18:32

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();

查看更多
别忘想泡老子
6楼-- · 2019-01-16 18:51

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.

查看更多
登录 后发表回答