Rajeev Singh
Rajeev Singh

Reputation: 504

How to get the current percentage utilization of each core of a processor using Java code

I have a situation where i need to get the utilization of each core of CPU. E.g. if I have a processor with 4 cores then the expected output is:

               Core-1          Core-2      Core-3        Core-4
Utilization    31.04           12.89       2.45          10.56 

I tried using following code:

OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
System.out.println(operatingSystemMXBean.getSystemLoadAverage());

The above code segment has a two fold problem. Firstly on Win7 32-bit the output is -1.0 and secondly the name of the method getSystemLoadAverage() gives an intution that it only displays overall cpu utilization.

Kindly help me with this issue as how to get the utilization of each core of processor.

Thanks

Upvotes: 1

Views: 592

Answers (2)

An SO User
An SO User

Reputation: 25028

You can use SIGAR. Their site says :

One API to access system information regardless of the underlying platform

I believe that is what you want. Have a look at their site: http://www.hyperic.com/products/sigar

JavaSysMon is another library to look at: https://github.com/jezhumble/javasysmon/wiki

Upvotes: 1

Martijn Courteaux
Martijn Courteaux

Reputation: 68907

You will have to write your application multithreaded. Writing a single-threaded application , is not faster on a quad core than on a single core (theoretically). To take advantage of your CPU cores, you have to explicitly program a process for each core.

http://www.tutorialspoint.com/java/java_multithreading.htm

Upvotes: 1

Related Questions