I'm currently building a Java app that could end up being run on many different platforms, but primarily variants of Solaris, Linux and Windows.
Has anyone been able to successfully extract information such as the current disk space used, CPU utilisation and memory used in the underlying OS? What about just what the Java app itself is consuming?
Preferrably I'd like to get this information without using JNI.
You can get some limited memory information from the Runtime class. It really isn't exactly what you are looking for, but I thought I would provide it for the sake of completeness. Here is a small example. Edit: You can also get disk usage information from the java.io.File class. The disk space usage stuff requires Java 1.6 or higher.
You can get some system-level information by using
System.getenv()
, passing the relevant environment variable name as a parameter. For example, on Windows:For other operating systems the presence/absence and names of the relevant environment variables will differ.
If you are using Jrockit VM then here is an other way of getting VM CPU usage. Runtime bean can also give you CPU load per processor. I have used this only on Red Hat Linux to observer Tomcat performance. You have to enable JMX remote in catalina.sh for this to work.
For windows I went this way.
Here is the link with details.
It is still under development but you can already use jHardware
It is a simple library that scraps system data using Java. It works in both Linux and Windows.
The java.lang.management package does give you a whole lot more info than Runtime - for example it will give you heap memory (
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()
) separate from non-heap memory (ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()
).You can also get process CPU usage (without writing your own JNI code), but you need to cast the
java.lang.management.OperatingSystemMXBean
to acom.sun.management.OperatingSystemMXBean
. This works on Windows and Linux, I haven't tested it elsewhere.For example ... call the get getCpuUsage() method more frequently to get more accurate readings.