java cpu使用率监控
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2062440/
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
java cpu usage monitoring
提问by shamkovich
Is there a way in monitoring CPU usage using pure Java?
有没有办法使用纯 Java 监控 CPU 使用率?
回答by kgiannakakis
回答by strainer
Maybe if stuck, you might 'sense' cpu availability by running an intermittent bogomips calculator in a background thread, and smoothing and normalising its findings. ...worth a shot no :?
也许如果卡住了,您可能会通过在后台线程中运行间歇性 bogomips 计算器并平滑和规范化其结果来“感知”cpu 可用性。...值得一试不:?
回答by strainer
There is a gem in the comments on the article which kgiannakakis linked:
kgiannakakis 链接的文章评论中有一个宝石:
JavaSysMon manages processes and reports useful system performance metrics cross-platform. You can think of it as a cross-platform version of the UNIX `top' command, along with the ability to kill processes. It comes in the form of a single JAR file /..
JavaSysMon 跨平台管理流程并报告有用的系统性能指标。您可以将其视为 UNIX `top' 命令的跨平台版本,以及终止进程的能力。它以单个 JAR 文件 /..
-works on Windows, Mac OS X, Linux, and Solaris.
- 适用于 Windows、Mac OS X、Linux 和 Solaris。
回答by baskin
How about using jmx mbeans?
使用 jmx mbeans 怎么样?
final OperatingSystemMXBean myOsBean=
ManagementFactory.getOperatingSystemMXBean();
double load = myOsBean.getSystemLoadAverage();
回答by Mark Jeronimus
You canuse jMX beans to calculate a CPU load. Note that this measures CPU load of your java program, not the overall system load. (the question didn't specify which)
您可以使用 jMX bean 来计算 CPU 负载。请注意,这是衡量 Java 程序的 CPU 负载,而不是整体系统负载。(问题没有具体说明哪个)
Initialize:
初始化:
ThreadMXBean newBean = ManagementFactory.getThreadMXBean();
try
{
if (this.newBean.isThreadCpuTimeSupported())
this.newBean.setThreadCpuTimeEnabled(true);
else
throw new AccessControlException("");
}
catch (AccessControlException e)
{
System.out.println("CPU Usage monitoring is not available!");
System.exit(0);
}
Then as your loop (assuming your application uses a loop, otherwise what's the point in measuring CPU usage?) use this:
然后作为你的循环(假设你的应用程序使用循环,否则测量 CPU 使用率有什么意义?)使用这个:
long lastTime = System.nanoTime();
long lastThreadTime = newBean.getCurrentThreadCpuTime();
while (true)
{
// Do something that takes at least 10ms (on windows)
try
{
int j = 0;
for (int i = 0; i < 20000000; i++)
j = (j + i) * j / 2;
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
// Calculate coarse CPU usage:
long time = System.nanoTime();
long threadTime = newBean.getCurrentThreadCpuTime();
double load = (threadTime - lastThreadTime) / (double)(time - lastTime);
System.out.println((float)load);
// For next iteration.
lastTime = time;
lastThreadTime = threadTime;
}
You need to use double precision because a long doesn't fit in a float (though it might work 99.9999999999999999% of the time)
您需要使用双精度,因为 long 不适合浮点数(尽管它可能在 99.9999999999999999% 的时间内有效)
If the 'something' you're doing takes less than approximately 1.6ms (Windows), then the returned value will not even have increased at all and you'll perpetually measure 0% CPU erroneously.
如果您正在做的“某事”花费的时间少于大约 1.6 毫秒(Windows),那么返回的值甚至根本不会增加,您将永远错误地测量 0% 的 CPU。
Because getCurrentThreadCpuTime
is VERY inaccurate (with delays less than 100ms), smoothing it helps a lot:
因为getCurrentThreadCpuTime
非常不准确(延迟小于 100 毫秒),平滑它有很大帮助:
long lastTime = System.nanoTime();
long lastThreadTime = newBean.getCurrentThreadCpuTime();
float smoothLoad = 0;
while (true)
{
// Do something that takes at least 10ms (on windows)
try
{
int j = 0;
for (int i = 0; i < 2000000; i++)
j = (j + i) * j / 2;
Thread.sleep(10);
}
catch (InterruptedException e)
{
}
// Calculate coarse CPU usage:
long time = System.nanoTime();
long threadTime = newBean.getCurrentThreadCpuTime();
double load = (threadTime - lastThreadTime) / (double)(time - lastTime);
// Smooth it.
smoothLoad += (load - smoothLoad) * 0.1; // damping factor, lower means less responsive, 1 means no smoothing.
System.out.println(smoothLoad);
// For next iteration.
lastTime = time;
lastThreadTime = threadTime;
}
回答by Devendar
if you are using linux - just use jconsole
- you will get all the track of java memory management
如果您使用的是 linux - 只需使用jconsole
- 您将获得 Java 内存管理的所有信息