Java 自 JVM 启动以来的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/817801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
time since JVM started
提问by flybywire
Is there a way to find out the time since the JVM started?
有没有办法找出自 JVM 启动以来的时间?
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.
当然,除了main
在 .
采纳答案by Yuval Adam
Use this snippet:
使用这个片段:
long jvmUpTime = ManagementFactory.getRuntimeMXBean().getUptime();
or:
或者:
long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
This is the correctway of retrieving JVM up-time.
这是检索 JVM 正常运行时间的正确方法。
For more info see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/RuntimeMXBean.html
有关更多信息,请参阅http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/RuntimeMXBean.html
回答by David Rabinowitz
You can get the start time of the JVM in the following code:
可以通过以下代码获取JVM的启动时间:
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.
查看更多是https://docs.oracle.com/javase/6/docs/api/java/lang/management/RuntimeMXBean.html。
回答by Gerco Dries
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.
从 Java 5 开始,您可以使用 JMX 来找出这一点。查看“使用平台 MBeanserver”以了解更多详细信息。您要查找的 bean 是名为“java.lang:type=Runtime”的 bean。属性 StartTime 为您提供 JVM 的启动时间,而 Uptime 属性则告诉您 JVM 的正常运行时间。
You can get a reference to the Runtime bean by executing this code:
ManagementFactory.getRuntimeMXBean();
您可以通过执行以下代码获取对 Runtime bean 的引用:
ManagementFactory.getRuntimeMXBean();
回答by Oversteer
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.
也许值得一提的是,如果您不想编写任何代码,请从 java bin 目录运行 jconsole,然后单击 VM Summary 并查看 Uptime 和 Process CPU 时间值。
回答by u3378901
if your jvm program running in linux, you can view the startTime use ps
如果你的jvm程序在linux下运行,可以使用ps查看startTime
ps -p <pid> -o stime,etime