Java on Windows needs to log CPU load and similar OS specific performance information

175 views Asked by At

There is a Java library running on windows machines that needs to log information about the OS like CPU load, memory occupied by the JVM etc. which I'm quite sure Java itself cannot obtain, because it is OS specific.

This information is needed in the logs of this library in order to point out to clients that certain operations failed because the library could not obtain enough resources.

It is not possible to choose the JVM, i.e. we cannot demand that our clients should use a specific JVM that does implement windows OS specific functionality.

Is there a windows library (DLL) or API that could be used via JNI?

We could also implement a DLL in C++ or C# ourselves, where would I need to look to see how this could be done most effectively?

Edit: I need access to data about the process of the JVM itself, which I can get through the native Windows API only, I guess. So I think I'll have to implement a small C program and link this as native DLL via JNI. Any tips on that?

2

There are 2 answers

1
loodakrawa On

For the memory part, look at several memory related methods of the Runtime. To get the occupied memory, try the following:

Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.totalMemory() - runtime.freeMemory());

I have not tried this yet, but for the CPU load take a look at the following. The getSystemLoadAverage() method should do it.

Hope this helps!

1
sceaj On

If your needs aren't met by java.lang.Runtime, you could go the JNI route and design a Java API that provides exactly what you need.

I don't know of any out of the box DLLs that you could wrap (with JNI) but the data you're after is in the Registry as the so-called "performance counters". On linux you could read the data from /proc.

See the MSDN site for more info on Performance Counters.