4

I am trying to monitor all running Java virtual machines.

Unfortunately I couldn't find a way to reliably retrieve the cpu usage (e.g. in percent) of specific processes or VMs.

I know about SIGAR, but the ProcCpu getPercent() call isn't reliable at all. (multiple invocations needed to get a result)

So far I managed to get a list of PIDs for all VMs.

Should be a cross-platform approach if possible (at least windows & linux).

edit: I took a closer look into the approach referenced by Fraser, but couldn't figure out how to access my VMs using

ObjectName.getInstance("java.lang:type=OperatingSystem");

If I am not mistaken, it's only possible to access (java) processes you started and registered yourself.

Community
  • 1
  • 1
Snu
  • 142
  • 1
  • 12
  • 1
    Duplicate? http://stackoverflow.com/questions/18489273/how-to-get-percentage-of-cpu-usage-of-os-from-java – Fraser Oct 15 '15 at 16:39
  • 1
    @Fraser I found this "solution" as well, but that approach isn't reliable in my opinion. – Snu Oct 15 '15 at 17:44
  • 1
    See [this answer](http://stackoverflow.com/a/19400008/2711488). It creates a `MemoryMXBean` for an arbitrary process, but you may easily adapt it for an `OperatingSystemMXBean`. – Holger Oct 19 '15 at 15:01
  • @Holger Thanks, very nice approach. Do you know how the different types of (Non-)HeapMemoryUsage are related to the actual RAM usage of the process? I couldn't figure out an exact match. (taskmanager, top) – Snu Oct 21 '15 at 08:09
  • Unfortunately the `OperatingSystenMXBean.getSystemLoadAverage()` returns -1 for me (under Windows). The average for the last minute doesn't provide enough detail for my concern anyway. – Snu Oct 21 '15 at 08:16
  • 1
    Well, [this answer](http://stackoverflow.com/a/21962037/2711488) uses dynamic attribute discovery as the `ProcessCpuLoad` is not a property of the [standard interface](http://docs.oracle.com/javase/8/docs/api/?java/lang/management/OperatingSystemMXBean.html). The same applies to the `SystemCpuLoad` property which provides a useful value for Windows and Oracle’s JVM. Regarding memory, I’m afraid you’ll never get exactly the same number as the task manager; even Process Explorer and Task Manager show different values and you can fill entire books with explanations of the differences… – Holger Oct 21 '15 at 10:07

0 Answers0