如何用Java监控计算机的CPU、内存和磁盘使用情况?
我想用 Java 监视以下系统信息:
- 当前 CPU 使用率**(百分比)
- 可用内存*(可用/总计)
可用磁盘空间(可用/总计)
*请注意,我的意思是整个系统可用的总内存,而不仅仅是 JVM。
我正在寻找一种跨平台解决方案(Linux、Mac 和 Windows),该解决方案不依赖于我自己的代码调用外部程序或使用 JNI。 尽管这些都是可行的选择,但如果有人已经有了更好的解决方案,我宁愿不自己维护特定于操作系统的代码。
如果有一个免费的库可以以可靠的跨平台方式执行此操作,那就太好了(即使它进行外部调用或本身使用本机代码)。
非常感谢任何建议。
澄清一下,我想获取整个系统的当前 CPU 使用情况,而不仅仅是 Java 进程。
SIGAR API 在一个包中提供了我正在寻找的所有功能,因此这是迄今为止我的问题的最佳答案。 然而,由于它是根据 GPL 许可的,我不能将它用于我的原始目的(闭源商业产品)。 Hyperic 可能会授权 SIGAR 用于商业用途,但我还没有研究过。 对于我的 GPL 项目,我将来肯定会考虑 SIGAR。
对于我当前的需求,我倾向于以下内容:
- 对于 CPU 使用情况,
OperatingSystemMXBean.getSystemLoadAverage() / OperatingSystemMXBean.getAvailableProcessors()
(每个 cpu 的平均负载) - 对于内存,
OperatingSystemMXBean.getTotalPhysicalMemorySize ()
和OperatingSystemMXBean.getFreePhysicalMemorySize()
- 对于磁盘空间,
File.getTotalSpace()
和File.getUsableSpace()
限制:
getSystemLoadAverage()
和磁盘空间查询方法仅在 Java 6 下可用。此外,某些 JMX 功能可能不适用于所有平台(即据报道 getSystemLoadAverage()
在 Windows 上返回 -1)。
虽然最初是根据 GPL 授权的,但它已更改为Apache 2.0,通常可用于闭源商业产品。
I would like to monitor the following system information in Java:
- Current CPU usage** (percent)
- Available memory* (free/total)
Available disk space (free/total)
*Note that I mean overall memory available to the whole system, not just the JVM.
I'm looking for a cross-platform solution (Linux, Mac, and Windows) that doesn't rely on my own code calling external programs or using JNI. Although these are viable options, I would prefer not to maintain OS-specific code myself if someone already has a better solution.
If there's a free library out there that does this in a reliable, cross-platform manner, that would be great (even if it makes external calls or uses native code itself).
Any suggestions are much appreciated.
To clarify, I would like to get the current CPU usage for the whole system, not just the Java process(es).
The SIGAR API provides all the functionality I'm looking for in one package, so it's the best answer to my question so far. However, due it being licensed under the GPL, I cannot use it for my original purpose (a closed source, commercial product). It's possible that Hyperic may license SIGAR for commercial use, but I haven't looked into it. For my GPL projects, I will definitely consider SIGAR in the future.
For my current needs, I'm leaning towards the following:
- For CPU usage,
OperatingSystemMXBean.getSystemLoadAverage() / OperatingSystemMXBean.getAvailableProcessors()
(load average per cpu) - For memory,
OperatingSystemMXBean.getTotalPhysicalMemorySize()
andOperatingSystemMXBean.getFreePhysicalMemorySize()
- For disk space,
File.getTotalSpace()
andFile.getUsableSpace()
Limitations:
The getSystemLoadAverage()
and disk space querying methods are only available under Java 6. Also, some JMX functionality may not be available to all platforms (i.e. it's been reported that getSystemLoadAverage()
returns -1 on Windows).
Although originally licensed under GPL, it has been changed to Apache 2.0, which can generally be used for closed source, commercial products.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
沿着我在这篇文章中提到的 。 我建议您使用 SIGAR API。 我在自己的一个应用程序中使用了 SIGAR API,它非常棒。 您会发现它很稳定,得到良好的支持,并且充满了有用的示例。 它是开源的,具有
GPL 2Apache 2.0 许可证。 一探究竟。 我有一种感觉它会满足你的需求。使用 Java 和 Sigar API,您可以获得内存、CPU、磁盘、平均负载、网络接口信息和指标、进程表信息、路由信息等。
Along the lines of what I mentioned in this post. I recommend you use the SIGAR API. I use the SIGAR API in one of my own applications and it is great. You'll find it is stable, well supported, and full of useful examples. It is open-source with a
GPL 2Apache 2.0 license. Check it out. I have a feeling it will meet your needs.Using Java and the Sigar API you can get Memory, CPU, Disk, Load-Average, Network Interface info and metrics, Process Table information, Route info, etc.
以下内容应该可以为您提供 CPU 和 RAM。 有关更多详细信息,请参阅 ManagementFactory 。
The following supposedly gets you CPU and RAM. See ManagementFactory for more details.
在 JDK 1.7 中,您可以通过
com.sun.management.OperatingSystemMXBean
。 这与 java.lang.management.OperatingSystemMXBean 不同。In JDK 1.7, you can get system CPU and memory usage via
com.sun.management.OperatingSystemMXBean
. This is different thanjava.lang.management.OperatingSystemMXBean
.2008 年接受的答案推荐了 SIGAR。 然而,正如 2014 年的评论 (@Alvaro) 所说:
我的建议是使用 https://github.com/oshi/oshi
或者上面提到的答案。
The accepted answer in 2008 recommended SIGAR. However, as a comment from 2014 (@Alvaro) says:
My recommendation is to use https://github.com/oshi/oshi
Or the answer mentioned above.
对于磁盘空间,如果您有 Java 6,则可以使用 getTotalSpace 和 Apache Commons IO 来实现这一点。
恐怕我不知道有什么跨平台的方法来获取 CPU 使用率或内存使用率。
For disk space, if you have Java 6, you can use the getTotalSpace and getFreeSpace methods on File. If you're not on Java 6, I believe you can use Apache Commons IO to get some of the way there.
I don't know of any cross platform way to get CPU usage or Memory usage I'm afraid.
其中很多内容已经可以通过 JMX 获得。 在 Java 5 中,JMX 是内置的,并且它们包含带有 JDK 的 JMX 控制台查看器。
您可以使用 JMX 手动监控,或者如果您在自己的运行时需要此信息,则可以从 Java 调用 JMX 命令。
A lot of this is already available via JMX. With Java 5, JMX is built-in and they include a JMX console viewer with the JDK.
You can use JMX to monitor manually, or invoke JMX commands from Java if you need this information in your own run-time.
import
com.sun.management.OperatingSystemMXBean
它仅在第二次调用后开始工作,因此保存 osBean 并将其放入循环中
import
com.sun.management.OperatingSystemMXBean
It only starts working after the second call so save the osBean and put it in a loop
制作一个批处理文件“Pc.bat”,
typeperf -sc 1 "\mukit\processor(_Total)\%% Processor Time"
您可以使用 MProcess 类,
}
然后经过一些字符串操作,您就可以获得 CPU 使用情况。 您可以对其他任务使用相同的流程。
——穆吉特·哈桑
Make a batch file "Pc.bat" as,
typeperf -sc 1 "\mukit\processor(_Total)\%% Processor Time"
You can use the class MProcess,
}
Then after some string manipulation, you get the CPU use. You can use the same process for other tasks.
--Mukit Hasan
以下代码仅适用于 Linux(也许是 Unix),但它可以在实际项目中运行。
The following code is Linux (maybe Unix) only, but it works in a real project.
这对我来说非常有效,无需任何外部 API,只需原生 Java 隐藏功能:)
This works for me perfectly without any external API, just native Java hidden feature :)
看看这篇非常详细的文章:
http://nadeausoftware.com/articles/2008/03/java_tip_how_get_cpu_and_user_time_benchmarking#UsingaSuninternalclasstogetJVM CPU时间
要获取 CPU 使用的百分比,您需要的只是一些简单的数学计算:
注意:您必须导入
com.sun.management.OperatingSystemMXBean
而不是java.lang.management.OperatingSystemMXBean
代码>.Have a look at this very detailled article:
http://nadeausoftware.com/articles/2008/03/java_tip_how_get_cpu_and_user_time_benchmarking#UsingaSuninternalclasstogetJVMCPUtime
To get the percentage of CPU used, all you need is some simple maths:
Note: You must import
com.sun.management.OperatingSystemMXBean
and notjava.lang.management.OperatingSystemMXBean
.