I'm currently writing a Java application that needs to look at how "heavily loaded" the machine it's running on is. On *nix, load average divided by number of processors fits the bill perfectly, and we retrieve load average with ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage(). Unfortunately, this returns -1 on Windows, as the call is apparently too "expensive" to be called frequently. What's the easiest way to retrieve similar Windows metrics such as the processor queue length or CPU utilisation, either in pure Java or via JNI?
3 Answers
You can retrieve the CPU utilization on Windows using WMI. Some code and documentation for accessing WMI from Java appears to be available here.
5 Comments
wmic is a native Windows scripting interface for WMI (if WMI exists on a host - so does wmic), just invoke it through your Java application.Try using the free Hyper SIGAR API. It is a cross platform API for calling system information. It uses JNI for Windows/Linux/Unix/Mac/etc.
http://www.hyperic.com/products/sigar
I wrote a JNLP task manager/information monitor with it and it's a decent API.
http://www.goliathdesigns.com/2009/12/sixth-post/
Source code:
Comments
You can also perfom this task using Eclipse SWT if you are running on an win32 environment:
http://dentrassi.de/2011/02/04/access-to-wmi-in-java-using-eclipse-swt-ole-integration/