tags:

views:

111

answers:

2

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?

+2  A: 

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:

http://sourceforge.net/projects/sysinfomonitor/

David Young
+2  A: 

You can retrieve the CPU utilization on Windows using WMI. Some code and documentation for accessing WMI from Java appears to be available here.

B.R.
Or even easier, use the wmic interface!
Anders
I've always been a fan of do it in code over invoking an external program, but wmic seems useful.
B.R.
WMI looks like what I'm after. I don't think I'll go down the J-Integra route, though - I'm thinking of using code from http://henryranch.net/software/jwmi-query-windows-wmi-from-java/ , although I'm still on the lookout for something that calls it programatically (this one creates a VBScript dynamically).
Scott