1

In containers running Java 21+, I would like to have the active processor count reflect the actual available hardware cores, regardless of configured CPU shares.

CSR JDK-8281571 "Do not use CPU Shares to compute active processor count" explains the problem with using Docker/Kubernetes CPU resource limits to determine the active processor count with -XX:+UseContainerSupport and I understand it to have changed this.

In Java 21 however, the deprecated flags controlling this (UseContainerCpuShares and PreferContainerQuotaForCPUCount) are gone, and I cannot find documentation on what the situation is supposed to be wrt container CPU limits.

The behavior in Kubernetes, with resource limits like this:

   resources:
      limits:
         cpu: 1000m

is that the active processor count is limited to 1 with all the problems described in JDK-8281571 (Runtime.getRuntime().availableProcessors() returns 1).

I could override this explicitly with -XX:ActiveProcessorCount=n, but this requires explicitly configuring the concrete processor count, which might change with node updates and tends to be missed during updates.

I tried setting -XX:ActiveProcessorCount=-1, however this does not do anything.

4
  • As far as I can tell, the ticket you link describes the problem of using requests, not limits to determine the CPU count. Commented Nov 5 at 10:43
  • @MarkRotteveel rereading, I can see that interpretation. I might have read too much into the discussion about how those limits are actually applied in the container runtime, and sentences like "After this CSR, the JVM may use as much CPU as given by the OS". I still find it strange - to put mildly - to derive a core count from a cpu time quota. Those are categorically different things. But I guess that's a different question. Commented Nov 5 at 12:44
  • The main question remains, however: Is there a simple way to make the JVM ignore the limit without setting a fixed core number manually (and without losing the other non-cpu UseContainerSupport comforts)? Commented Nov 5 at 12:45
  • I agree with you, on the other hand, telling Java there are a lot of CPUs while you have relatively low limits compared to the total CPU availability might lead to other problems due to throttling. But yes, I wonder how many applications are out there that don't run as well as they could because they assume a single CPU, and thus have small thread pools (which especially with virtual threads can be either a bottleneck, or even an issue if there is pinning). Commented Nov 6 at 9:26

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.