Published

How a 4 GB server froze with no OOM kill

The contact form on this site once sat on "Sending" for minutes. Saving a message is cheap: a rate-limit check, one call to Cloudflare, one MongoDB insert. So the request itself was not the problem. The whole machine had stopped.

Nothing looked like a memory problem at first, because the usual proof was missing: the kernel log had no OOM kill. Here is how the logs pointed to memory anyway, and what the failure teaches about running the JVM in containers.

Three lines that told the story

The application log had lines like these, close together:

HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=5m20s).
MongoDB heartbeat timed out
Turnstile rejected token: [timeout-or-duplicate]

Each one is ordinary alone. Together they describe one event.

  • The Hikari warning comes from a background thread that wakes up every 30 seconds and compares clocks. A delta of 5 minutes 20 seconds means that thread didn't run for over five minutes. Hikari wasn't the victim of a slow query. The JVM wasn't getting to run at all.
  • The MongoDB heartbeat timed out at the same moment, for the same reason.
  • The Turnstile rejection was the visitor's request finally reaching Cloudflare, long after the token in it had expired.

A single slow dependency produces errors about that dependency. Everything being late at once points to the host.

Why there was no OOM kill

The machine confirmed it. free -h showed 3.7 GB of RAM, about 130 MB free and no swap. docker stats hung when I ran it.

Most people expect Linux to kill something when memory runs out. It does, but only when it can no longer reclaim anything. A machine has a lot of memory it can still reclaim: file-backed pages, including the code of every running program. With no swap, anonymous memory has nowhere to go, so the kernel keeps throwing out those file pages and reading them back from disk a moment later.

Reclaim is technically succeeding, so the OOM killer has no reason to act. Every process is simply waiting on the disk to get its own code back. That is thrashing, and on a machine like this it looks exactly like a freeze.

So a quiet kernel log doesn't clear memory as a suspect.

How the memory added up

No single process was badly behaved. Each one chose a sensible default, and all of them assumed they had the machine to themselves.

Process Default sizing On 3.7 GB
Each JVM Heap up to 25% of the RAM it can see About 0.9 GB, before metaspace, threads and buffers
MongoDB WiredTiger cache of 50% of (RAM − 1 GB) About 1.3 GB
Postgres, admin tools and other services Their own The rest, and more

There were two JVMs on the host, the site and its config server, so the defaults alone asked for more than the machine had.

The fix is a budget

Memory on a shared host has to be divided on purpose. The JVM part is the one people get wrong:

services:
  server:
    mem_limit: 768m
    environment:
      JAVA_TOOL_OPTIONS: -XX:MaxRAMPercentage=60 -XX:+ExitOnOutOfMemoryError

The two settings only work as a pair. MaxRAMPercentage is a percentage of whatever the JVM believes its memory is. With a container limit, that's the limit. Without one, it is the whole host, and 60% of the host is worse than the default.

The percentage also isn't 100 because the heap is only part of a Java process. Metaspace, thread stacks, the code cache and direct buffers all live outside it. ExitOnOutOfMemoryError makes the process exit cleanly when the heap is exhausted, so the container restarts instead of limping along.

MongoDB needs its own cap, because it sizes its cache from the host:

command: ["mongod", "--wiredTigerCacheSizeGB", "0.25"]

On a small site that one flag hands back about a gigabyte. Then add swap, even a small amount with a low vm.swappiness. A little swap gives the kernel somewhere to put idle memory, so a spike slows the machine down instead of stopping it.

What I'd check first next time

  1. Look for everything being late at once. A Hikari housekeeper delta in minutes is a strong signal on its own.
  2. Run free -h and check swap before trusting a clean kernel log.
  3. Use ps -eo rss,comm --sort=-rss | head when docker stats hangs. ps reads /proc directly and still answers.
  4. Add up what each process is allowed to take, not what it is using right now. If the total is more than the machine, the freeze is only waiting for a busy moment.