Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Runtime.getRuntime().availableProcessors() and nproc return different values

Tags:

java

docker

I'm running a Java app inside of a docker container, which is restricted to 4 CPU and the machine on which the docker container runs, has 10 CPU.

When calling nproc inside the docker container, I get as a result 4, but when I call Runtime.getRuntime().availableProcessors() I get 10 as result.

  • why is this happening? How come does Java see all CPU?
  • is there another way of getting in Java the same result asa nproc, besides using Runtime.getRuntime().exec("nproc")?
like image 311
Paul Avatar asked Jun 20 '26 08:06

Paul


1 Answers

According to Ken Sipe (link below) there is a bug in java for showing the wrong no of cpus/cores when using shared cpus (default in docker) instead of cpusets (pinned cpus for a process).

I myself however see the wrong number regardless this setting:

Runtime.availableProcessors(), no docker: 8 in docker I always get 4,

whereas nproc says 1 (when using --cpuset-cpus=0 <- pinned on cpu0)

see https://vimeo.com/138955223

like image 114
Sander Hautvast Avatar answered Jun 21 '26 22:06

Sander Hautvast