Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Jonas

like image 469
Jonas Avatar asked Apr 23 '12 01:04

Jonas


2 Answers

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
like image 50
sarnold Avatar answered Sep 18 '22 11:09

sarnold


In Windows it's possible to change the number of cores an application is allowed to use via some simple properties. In virtualized environments you can also restrict cores to the VM so you only see the cores the VM sees. And in Linux/Unix you can set core affinity so that applications or users can't use all the cores so it's possible that you may have more cores installed than are visible to the JVM.

Note: this is useful for machines running multiple jobs where you don't want high resource contention and you want to underprovision (or even overprovision) resources.

like image 22
Jesus Ramos Avatar answered Sep 17 '22 11:09

Jesus Ramos