4

I have searched all over the internet for about two days now. I have found a lot about how to find the CPU usage using VB and anything in the .NET framework. I am not using the .NET framework so these are irrelevant, I feel.

What I really need is results similar to the ones shown in the "CPU Usage" section in a Windows Task Manager under the "Performance" tab. If I could find a way to mimic that calculation, then that is exactly what I am looking for. I need the percentage of CPU Usage.

Obviously, Java is not the ideal language for finding the CPU usage of a computer, but that is the language I am using and I have been able to get everything else I need about the computer except this.

I am using Windows 7 with Java version 7 update 21. Eclipse is my IDE.

This is my first time posting a question on here, so if I have left anything out or something is not clear, please ask and I will do my best to explain more of what I mean.

2
  • stackoverflow.com/questions/47177/… possible duplicate Commented May 30, 2013 at 15:54
  • @DGomez I tried using the SIGAR API that is mentioned in that answer that you posted, and I was able to get the memory of the computer and a few other things, but I am still not sure how to get the average CPU Usage. Maybe I just need some help in SIGAR API. What do you think? Commented May 30, 2013 at 16:02

1 Answer 1

3

There are a couple of ways to do this; one is to run a command-line tool and parse the output. On Windows 7, you can run

c:\Windows\system32\typeperf "\processor(_total)\% processor time"

Try it -- it prints a few lines of sampled CPU load data.

Another way is to use OperatingSystemMXBean .

Sign up to request clarification or add additional context in comments.

4 Comments

That is a very nice command! Since it causes the output to come out continually, how could I change it to just out put one line only when I call it?
You can add "-sc 1" to print just one sample.
This was exactly what I needed. Thank you for responding so quickly. I have marked this as the answer. I am going to parse through the output, as you said.
The OperatingSystemMXBean method does not work on my Windows 8 machine. I like your command-line solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.