Stop cgroups from lying to your Java apps about memory

Container Basics On Linux (Namespaces/Cgroups)

Stop cgroups from lying to your Java apps about memory

🧩 The Challenge

You deploy a container with a 2GB memory limit, but the Java process inside keeps crashing with an OOM error because it thinks it has access to the host’s 64GB of RAM. It’s infuriating to watch the kernel kill your process when you thought you had plenty of headroom.

💡 The Fix

Tell the JVM to respect the cgroup memory controller boundaries so it doesn’t try to allocate heap space that doesn’t actually exist. This ensures the app self-regulates before the kernel gets grumpy and sends a SIGKILL.

java -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -jar your-app.jar

⚙️ Why It Works

Adding these flags forces the JVM to parse the cgroup v1 or v2 memory limits instead of querying the host hardware via /proc/meminfo. Without them, the runtime ignores the container isolation and behaves like it’s running bare-metal.

🚀 Pro-Tip: Check /sys/fs/cgroup/memory/memory.limit_in_bytes if you ever want to verify exactly what limit the kernel is enforcing on your container.

Linux Tips & Tricks | © ngelinux.com | 9/2/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted