javajvmcpu-coresprocessors

Java: Cores available to the JVM?


In Java there's the method Runtime.getRuntime().availableProcessors() which has the following Javadoc:

Returns the number of processors available to the Java virtual machine. This value may change during a particular invocation of the virtual machine.

How can the value actually change? Under what circumstances would there be less processors available to the JVM than physically installed for example?


Solution

  • The Linux command taskset(1) can be used to force processes to use a specific CPU or specific sets of CPUs; it is pretty easy to modify a running program to force it to one or more processors. For example,

    taskset -p `pidof java` --cpu-list 0,5,7,9-11