如何在 Java 中监控计算机的 CPU、内存和磁盘使用情况? [英] How do I monitor the computer's CPU, memory, and disk usage in Java?

查看:113
本文介绍了如何在 Java 中监控计算机的 CPU、内存和磁盘使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java监控以下系统信息:

I would like to monitor the following system information in Java:

  • 当前 CPU 使用率**(百分比)
  • 可用内存*(可用/总)
  • 可用磁盘空间(可用/总)

  • Current CPU usage** (percent)
  • Available memory* (free/total)
  • Available disk space (free/total)

*请注意,我指的是整个系统可用的总内存,而不仅仅是 JVM.

*Note that I mean overall memory available to the whole system, not just the JVM.

我正在寻找一种跨平台解决方案(Linux、Mac 和 Windows),它不依赖于我自己的代码调用外部程序或使用 JNI.尽管这些都是可行的选择,但如果有人已经有了更好的解决方案,我宁愿不要自己维护特定于操作系统的代码.

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).

非常感谢任何建议.

为了澄清,我想获取整个系统的当前 CPU 使用率,而不仅仅是 Java 进程.

To clarify, I would like to get the current CPU usage for the whole system, not just the Java process(es).

SIGAR API 在一个包中提供了我正在寻找的所有功能,因此它是迄今为止对我的问题的最佳答案.但是,由于它是根据 GPL 获得许可的,我不能将它用于我的原始目的(封闭源代码的商业产品).Hyperic 可能会许可 SIGAR 用于商业用途,但我还没有研究过.对于我的 GPL 项目,我以后肯定会考虑 SIGAR.

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:

  • 对于 CPU 使用率,OperatingSystemMXBean.getSystemLoadAverage()/OperatingSystemMXBean.getAvailableProcessors()(每个 CPU 的平均负载)
  • 对于内存,OperatingSystemMXBean.getTotalPhysicalMemorySize()OperatingSystemMXBean.getFreePhysicalMemorySize()
  • 对于磁盘空间,File.getTotalSpace()File.getUsableSpace()
  • For CPU usage, OperatingSystemMXBean.getSystemLoadAverage() / OperatingSystemMXBean.getAvailableProcessors() (load average per cpu)
  • For memory, OperatingSystemMXBean.getTotalPhysicalMemorySize() and OperatingSystemMXBean.getFreePhysicalMemorySize()
  • For disk space, File.getTotalSpace() and File.getUsableSpace()

限制:

getSystemLoadAverage() 和磁盘空间查询方法仅在 Java 6 下可用.此外,某些 JMX 功能可能不适用于所有平台(即据报道 getSystemLoadAverage() 在 Windows 上返回 -1).

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).

虽然最初在 GPL 下获得许可,但它已更改Apache 2.0,一般可以用于闭源,商业产品.

Although originally licensed under GPL, it has been changed to Apache 2.0, which can generally be used for closed source, commercial products.

推荐答案

与我提到的一样 在这篇文章中.我建议您使用 SIGAR API.我在自己的一个应用程序中使用了 SIGAR API,它很棒.你会发现它很稳定,得到很好的支持,并且充满了有用的例子.它是开源的,具有 GPL 2 Apache 2.0 许可证.一探究竟.我感觉它会满足您的需求.

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 2 Apache 2.0 license. Check it out. I have a feeling it will meet your needs.

使用 Java 和 Sigar API,您可以获得内存、CPU、磁盘、平均负载、网络接口信息和指标、进程表信息、路由信息等.

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.

这篇关于如何在 Java 中监控计算机的 CPU、内存和磁盘使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆