[英]java cpu usage monitoring
有没有办法使用纯Java监视CPU使用率?
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.
JavaSysMon管理流程并跨平台报告有用的系统性能指标。 You can think of it as a cross-platform version of the UNIX `top' command, along with the ability to kill processes.
您可以将其视为UNIX`top'命令的跨平台版本,以及杀死进程的功能。 It comes in the form of a single JAR file /..
它以单个JAR文件/ ..的形式出现。
-works on Windows, Mac OS X, Linux, and Solaris. -可在Windows,Mac OS X,Linux和Solaris上运行。
How about using jmx mbeans? 如何使用jmx mbeans?
final OperatingSystemMXBean myOsBean=
ManagementFactory.getOperatingSystemMXBean();
double load = myOsBean.getSystemLoadAverage();
You can use jMX beans to calculate a CPU load. 您可以使用jMX bean来计算CPU负载。 Note that this measures CPU load of your java program, not the overall system load.
请注意,这衡量的是Java程序的CPU负载,而不是总体系统负载。 (the question didn't specify which)
(问题未指定哪个)
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) 您需要使用双精度,因为长整型不适合浮点数(尽管它可能在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
非常不准确(延迟小于100ms),因此对其进行平滑处理将有很大帮助:
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;
}
Maybe if stuck, you might 'sense' cpu availability by running an intermittent bogomips calculator in a background thread, and smoothing and normalising its findings. 也许卡住了,您可以通过在后台线程中运行间歇性Bogomips计算器并平滑和归一化其发现来“感知” cpu的可用性。 ...worth a shot no :?
...值得一试:
如果您使用的是jconsole
只需使用jconsole
您将获得有关Java内存管理的所有信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.