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

查看:1450
本文介绍了如何在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.

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

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天全站免登陆