Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I monitor the computer's CPU, memory, and disk usage in Java?
    primarykey
    data
    text
    <p>I would like to monitor the following system information in Java:</p> <ul> <li>Current CPU usage** (percent)</li> <li>Available memory* (free/total)</li> <li><p>Available disk space (free/total)</p> <p>*Note that I mean overall memory available to the whole system, not just the JVM.</p></li> </ul> <p>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.</p> <p>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).</p> <p>Any suggestions are much appreciated.</p> <p>To clarify, I would like to get the current CPU usage for the whole system, not just the Java process(es).</p> <p>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.</p> <p>For my current needs, I'm leaning towards the following:</p> <ul> <li>For CPU usage, <code>OperatingSystemMXBean.getSystemLoadAverage() / OperatingSystemMXBean.getAvailableProcessors()</code> (load average per cpu)</li> <li>For memory, <code>OperatingSystemMXBean.getTotalPhysicalMemorySize()</code> and <code>OperatingSystemMXBean.getFreePhysicalMemorySize()</code></li> <li>For disk space, <code>File.getTotalSpace()</code> and <code>File.getUsableSpace()</code></li> </ul> <p>Limitations:</p> <p>The <code>getSystemLoadAverage()</code> 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 <code>getSystemLoadAverage()</code> returns -1 on Windows).</p> <p>Although originally licensed under GPL, it <a href="http://forums.hyperic.com/jiveforums/thread.jspa?threadID=11426&amp;tstart=0" rel="noreferrer">has been changed</a> to <a href="http://support.hyperic.com/display/SIGAR/Home#Home-license" rel="noreferrer">Apache 2.0</a>, which can generally be used for closed source, commercial products.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload