Meaningless Drivel is fun!
The moose likes Other Open Source Projects and the fly likes Calculation of CPU load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Calculation of CPU load " Watch "Calculation of CPU load " New topic

Calculation of CPU load

Abhijit Das
Ranch Hand

Joined: Sep 25, 2007
Posts: 156
I want to calculate the CPU load using a single process in JAVA. The load can be in % and as shown in the task manager (CPU usage) in Windows.

Can you give me hint or brief notes on it.


Abhijit Das
SCJP 5.0 | SCWCD 1.5
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

There's no way to calculate it, but you can certainly ask Windows what it is. There are multiple ugly ways you might do that. For example, on Windows XP Pro (and presumably some of your more Pro-life versions of Vista, as well) there's a program called "tasklist" which reports process info to standard output. You could run it with a Runtime.exec() call and extract the number(s) you want from the output.

You could also use JNI, the Java Native Interface, to call the appropriate Win32 function. You might be able to think of some even more hideous ways to do it.

Or you could use the right tool for the job, and use a Windows language like C#, C++, etc, to write the whole thing and talk to the API directly.

[Jess in Action][AskingGoodQuestions]
Abhijit Das
Ranch Hand

Joined: Sep 25, 2007
Posts: 156
Thanks for the reply.
I have to use native method which is written in C++, etc to get the CPU load.

How can I use the JNI API to accomplish the task ?

Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

How can I use the JNI API to accomplish the task ?

JNI is just a way to call native programs from java. So, the assumption is that you have written something in C/C++ to talk to the api for getting CPU usage details. JNI will just help you to call this C/C++ program from Java and get the results.
[ December 08, 2008: Message edited by: Nitesh Kant ]

apigee, a better way to API!
Henry Wong

Joined: Sep 28, 2004
Posts: 20514

An easier way is probably to do a runtime.exec() to the typeperf command. This command report on OS counters -- including the counters for the CPU usage.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abhijit Das
Ranch Hand

Joined: Sep 25, 2007
Posts: 156
Hi friends,

I have come across API of JRobin to calculate the CPU load. It takes the support of NET-SMPT jars. But I cannot able to configure the SMPT. Resgistration and Start service are done . But the log files show it is not properly started.
If any body use these two JARs to acomplish the CPU load, then please help to configure all.

While running the main function , Timeout exception is comming.

Martijn Verburg

Joined: Jun 24, 2003
Posts: 3275

Moving to Open Source forum now.

Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Peter Lawrey
Ranch Hand

Joined: Dec 21, 2008
Posts: 62
You can use
to get the CPU time consumed by threads.

If you use a Custom ThreadGroup or poll you Thread periodically, you can collect this information for Threads which die.
I agree. Here's the link:
subject: Calculation of CPU load
It's not a secret anymore!