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
Using Java to retrieve the CPU Usage for Window's Processes
提问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
It allows access to the info you want (and much more) and is supported on many relevant platforms
它允许访问您想要的信息(以及更多信息)并在许多相关平台上受支持
回答by bmargulies
回答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 /proc
but not on Win, a separate proggy would be needed (without JNI).
您正在编写/使用单独的程序将数据转储到某个地方,然后可以通过网络、文件或数据库从 Java 中提取它。在 Linux 中你总是可以阅读/proc
但不能在 Win 上阅读,需要一个单独的 proggy(没有 JNI)。
回答by Dave Webb
回答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。