4 Replies Latest reply: May 27, 2011 10:14 PM by jtahlborn RSS

    How to get a System's total CPU usage percentage

    829000
      Hi,

      I need to find the system's total CPU usage percentage ( as we see in Windows Task Manager) using a java program. I am able to find the CPU usage time for one process using the OperatingSystemMXBean API. But i am not able to find a way to find the CPU usage percentage for all the process running on a system.
      I cannut use any third party tools to acheive this. Are there any third party jars available which can help me in getting this data?

      Thanks
      Prachi
        • 1. Re: How to get a System's total CPU usage percentage
          sabre150
          One possibility is to use ProcessBuilder to execute

          "tasklist /FO CSV"

          and then parse the 'stdout'. You might find the CPU time resolution too small for you but ...

          Note - you should read the 4 sections of http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations. Though the article is written round Runtime.exec() the traps apply to the Process object however it is created.
          • 2. Re: How to get a System's total CPU usage percentage
            833851
            i guess jniwrapper libs allow you to do such thing. however it will be native impl and jni calls.
            on a side note, do consider # of processors as well. these days most of the machines will have multiple cores.

            cheers
            ameyas7
            • 3. -deleted-
              BIJ001
              (deleted)

              Edited by: BIJ001 on 27-May-2011 05:39
              • 4. Re: -deleted-
                jtahlborn
                you might want to check out the SIGAR library, http://support.hyperic.com/display/SIGAR/Home . we were looking for functionality like this and came across this library. in the testing i did, it seemed to work great, although we never ended up implementing the features which required it, so i never actually used it for real.