6

with

Runtime.getRuntime().availableProcessors();

I can get the numbers of CPU avaiable.

But how can I get their clock frequency?

I was searching for a os-indipendent solution.

3
  • This is OS-specific. Which OS are you running ? Sep 19, 2009 at 15:17
  • Probably only platform-specific solutions exist (read /proc/cpuinfo?). But in general - it's not easy to tell the freq anymore... do you mean the current frequency? or the max frequency? and what about not limited VMs sharing a host with max 4GHz? (do 4 such hosts have 4GHz, or 1GHz each? or maybe the current idle value of 80MHz)
    – viraptor
    Sep 19, 2009 at 15:20
  • I don't know what you want to use this information for, but note that the clock frequency does not correspond to how fast the processor can do computations. A 3 GHz Pentium 4 for example is slower than a 2.4 GHz Core 2 Duo.
    – Jesper
    Sep 19, 2009 at 21:51

1 Answer 1

7

There is no feature available in the Java SE API which will return the frequency of the CPU.

Also, the Runtime.availableProcessors method returns the number of processors available to the Java virtual machine at the time the method is called, so it won't necessarily return the number of actual processors in a system.

From the Java API Specification for the Runtime.availableProcessors method:

Returns the number of processors available to the Java virtual machine.

This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.

In order to obtain such information, most likely, one would have to make calls the operating system using native calls via Java Native Interface (JNI).

2
  • Thank's. Very clear. I was searching for native method, but if doesn't exist I will study JNI. ;) Sep 19, 2009 at 15:38
  • This is not intended to model the underlying hardware exactly but to give a rough idea of the number of concurrent threads the hardware can support. Aug 9, 2013 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.