Video in progress

Strong Middle ⚙️ JVM and memory

What does the difference between cgroup v1 and v2 mean for the JVM, and why does an old JDK see the wrong memory limit on new nodes?

The JVM reads container limits from cgroup files, and v1 and v2 have different files and structure. Only JDK 15+ and the backported 11.0.16+ and 8u372+ understand cgroup v2. An older JVM can't find the limit on a v2 node and sizes the heap from the memory of the whole node.

📖 Answer

Many distributions (Ubuntu 22.04+, RHEL 9, new Kubernetes node images) switched to cgroup v2 by default. To check which one you have:

stat -fc %T /sys/fs/cgroup/      # cgroup2fs means v2, tmpfs means v1

The difference for the JVM:

  • v1: memory/memory.limit_in_bytes, cpu/cpu.cfs_quota_us;
  • v2: a single hierarchy, memory.max, cpu.max.

A classic incident: the nodes were upgraded, the applications didn't change, but the services on 11.0.10 started getting OOMKilled. The JVM doesn't know v2, so it considers the limit "absent", MaxRAMPercentage=75 is now 75% of a 64 GB node, and the heap tries to grow up to 48 GB while the pod limit is 2 GB. The CPU count also looks like the node's.

To check what the JVM sees:

java -XshowSettings:system -version
java -Xlog:os+container=debug -version

The first prints something like "Operating System Metrics: Provider: cgroupv2, Memory Limit: 2.00G". If you see "No container detected" or a wrong number, that is the problem.

The fixes: upgrade the JDK (the proper one), or temporarily set -Xmx and -XX:ActiveProcessorCount explicitly. Checking the JDK version in the base image together with every node OS upgrade should be on the platform team's checklist.