windows 使用 Java 检索 Window 进程的 CPU 使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1767271/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:28:12  来源:igfitidea点击:

Using Java to retrieve the CPU Usage for Window's Processes

javawindowsprocesscpusystem-monitoring

提问by Stephen Watkins

I am looking for a Java solution to finding the CPU usage for a running process in Windows. After looking around the web, there seems to be little information on a solution in Java. Keep in mind, I am not looking to find the CPU usage for the JVM, but any process running in Windows at the time.

我正在寻找一个 Java 解决方案来查找 Windows 中正在运行的进程的 CPU 使用率。环顾网络后,似乎很少有关于 Java 解决方案的信息。请记住,我不是要查找 JVM 的 CPU 使用率,而是要查找当时在 Windows 中运行的任何进程。

I am able to retrieve the memory usage in Java by using the exec("tasklist.exe ... ") to retrieve and parse process information. Although there is an aggregate CPU cycle timer for each process, I do not see a CPU usage column.

我可以通过使用 exec("tasklist.exe ... ") 来检索和解析进程信息来检索 Java 中的内存使用情况。虽然每个进程都有一个聚合的 CPU 周期计时器,但我没有看到 CPU 使用率列。

Any help would be greatly appreciated. Also, if possible, I would like to stay away from C libraries; however, if there is no other alternative, a solution by that means would be appropriate.

任何帮助将不胜感激。另外,如果可能的话,我想远离 C 库;但是,如果没有其他选择,通过这种方式的解决方案将是合适的。

Thanks a lot,

非常感谢,

Steve

史蒂夫

采纳答案by jitter

If a GPL licensed API is fine for you, try out

如果 GPL 许可的 API 适合您,请尝试

SIGAR - System Information Gatherer And Reporter

SIGAR - 系统信息收集器和报告器

It allows access to the info you want (and much more) and is supported on many relevant platforms

它允许访问您想要的信息(以及更多信息)并在许多相关平台上受支持

回答by bmargulies

Look into Jacob. It will let you call COM objects, and there are COM objects galore for this kind of information on Windows.

看着雅各布。它将让您调用 COM 对象,并且在 Windows 上有大量的 COM 对象可用于此类信息。

回答by zeroin23

Try out javasysmon, BSD licensed (to be precise: the FreeBSD 2-clause license).

试试javasysmon,BSD 许可(准确地说:FreeBSD 2-clause 许可)。

I used it to obtain CPU core and CPU Usage. It is much better than using tasklist.exe I think.

我用它来获取 CPU 内核和 CPU 使用率。我认为它比使用 tasklist.exe 好得多。

回答by Jé Queue

You be writing/using a separate program to dump that data somewhere and could then pick it up from Java through either network, files, or DB. In Linux you could always read /procbut not on Win, a separate proggy would be needed (without JNI).

您正在编写/使用单独的程序将数据转储到某个地方,然后可以通过网络、文件或数据库从 Java 中提取它。在 Linux 中你总是可以阅读/proc但不能在 Win 上阅读,需要一个单独的 proggy(没有 JNI)。

回答by BalusC

any running process in Windows

Windows 中的任何正在运行的进程

Thus, not controlled by Java in any way?

因此,不受Java 以任何方式控制?

I would like to stay away from JNI

我想远离JNI

Forget it. Java isn't platform specific. If you want to access platform specific information, you can't go around JNI.

算了吧。Java 不是特定于平台的。如果您想访问特定于平台的信息,则不能绕过 JNI。