0

I'd like to write a little program just to display the CPU usage as a percent, like the task manager does. I know intermediate C++ and Java. Is there a way to do this with either language? If so, perhaps a short example? I saw some page of a C++ command, but I couldn't make heads or tails of it.

I'm using Windows 7 on one computer and XP on the other. As for the multiple core response, I simply want to display the CPU usage percent as the task manager does, even with a multiple core processor.

rphello101
  • 1,671
  • 5
  • 31
  • 59

7 Answers7

5
double sysLoad = ManagementFactory.
                         getOperatingSystemMXBean().
                                 getSystemLoadAverage();
  • does not work on every platform,
  • returns an average load of the past one minute

EDIT: Recently found this one

http://sellmic.com/blog/2011/07/21/hidden-java-7-features-cpu-load-monitoring/

In Java 7 you can use com.sun.management package to get process load or system load.

usage:

OperatingSystemMXBean osBean = ManagementFactory
        .getPlatformMXBean(com.sun.management.OperatingSystemMXBean.class);  

// What % CPU load this current JVM is taking, from 0.0-1.0  
System.out.println(osBean.getProcessCpuLoad());  

// What % load the overall system is at, from 0.0-1.0  
System.out.println(osBean.getSystemCpuLoad());  
drzymala
  • 2,009
  • 20
  • 26
1

there are some system call functions provided in "windows.h", such as GetProcessorInfo(), CallNTPowerInformation(), GetTickCount(), QueryPerformanceFrequency(), QueryPerformanceCounter() and so on. You can google them, or find them in MSDN. I hope this answer can help you.

Kerwong
  • 398
  • 1
  • 4
  • 7
1

The answer is going to be platform specific, and differs between the Java and C++ cases. At some level you need to interface with OS specific APIs to extract the statistics. There are four possible approaches:

  • Call an appropriate external system monitoring command (system specific) and "scrape" the output of the command. You can do this from Java or C++.

  • In C++ (or using JNI / JNA in Java ... if you really have to), make calls on the OS-specific native APIs that provide the information used by the external system monitoring.

  • In Java, use some existing 3rd-party library that deals with the system specific JNI/JNA stuff. Possible examples include JavaSysMon and SIGAR ... though I can't make specific recommendations.

  • You could use the Java monitoring APIs ... but they don't provide enough information. You can get information about this processes resource usage, and the overall load average, but you can't get information about other processes.

Now the problem with all of the above (except, possibly, the last one) is that porting to a new platform requires work.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

CPU usage is an operating system property, not a language property. The way to retrieve it would be specific to the OS you're using (which you fail to identify).

In addition, "CPU usage" is a very nebulous term anymore, with multiple cores, et al.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

In Java you can do it using JavaSysMon

RoneRackal
  • 1,243
  • 2
  • 10
  • 16
0

Assuming you're using Windows system, you can use Windows Management Instrumentation (WMI). It's powerful once you get it working. I never did it using Java. Of course, it's easier with C# .NET. A good link is WMI info

If you try this, please tell me. I might be interested in helping you since this is also my interest.

The Original Android
  • 6,147
  • 3
  • 26
  • 31
0

IF you are using the Linux system, consider using shell scripting, like bash. The reason is shell scripting is powerful for operating system calls, like getting process ID and usage (pid command). And IT technicians are more comfortable with bash scripts than Java or C++.

The Original Android
  • 6,147
  • 3
  • 26
  • 31