Why Node.js containers get OOMKilled
Node.js OOMKilled happens when a container's memory usage exceeds its cgroup limit and the Linux kernel's OOM killer sends SIGKILL, which Docker and Kubernetes report as exit code 137. In client projects I've fixed this by confirming the kill source with docker inspect or kubectl describe pod, then matching the container's memory limit to what V8 and Node actually need, instead of just raising the limit blindly.
I've debugged this exact crash loop in fintech backends, API gateways, and worker queues running under Docker and Kubernetes. The symptom is always the same: the app runs fine locally, then dies in staging or production the moment real traffic hits it. The cause is almost never "Node needs more RAM" — it's usually a mismatch between what the container promises and what the runtime assumes it has.
What exit code 137 actually means
Exit code 137 is 128 + 9, where 9 is the SIGKILL signal number. It doesn't only mean OOM — a manual kill -9 or an unrelated host kill produces the same code. The way you tell them apart is the OOMKilled flag Docker and Kubernetes report separately from the exit code itself, as both the container exit code reference and Kubernetes' Pod troubleshooting guide confirm: a Reason: OOMKilled entry in kubectl describe pod means the kernel killed the container for exceeding resources.limits.memory, or because the node itself ran out of memory.
How to diagnose a Node.js OOMKilled crash
Don't guess. Confirm the kill source first, then look at the memory trend before the crash, then check how Node sized its own heap relative to the container.
Step 1: Confirm it was really OOM
# Docker
docker inspect my-service --format '{{.State.OOMKilled}} {{.State.ExitCode}}'
# Kubernetes
kubectl describe pod my-pod | grep -A 5 "Last State"
kubectl get events --field-selector reason=OOMKilling
If OOMKilled is false but the exit code is still 137, check dmesg or journalctl -k on the host for a node-level OOM event — that's a different problem with a different fix (usually under-provisioned nodes, not your app).
# What limit did the container actually get?
docker inspect my-service --format 'mem={{.HostConfig.Memory}}'
# Live usage against that limit
docker stats --no-stream my-service
In Kubernetes, compare kubectl top pod --containers against resources.limits.memory in the pod spec. If usage sits right at the ceiling before every crash, that's your answer — the limit is too tight for the real working set.
Step 3: Check how Node sized its own heap
This is the step most teams skip, and it's the one that actually explains "why does it crash even though the limit looks fine."
Why Node.js ignores your container's memory limit (sometimes)
Node has been container-aware since Node.js 12, meaning it reads the cgroup memory limit and tells V8 how much heap it can use instead of reading the host's total RAM. But that detection only works reliably if your runtime and cgroup version line up.
According to Kubernetes' cgroup v2 documentation, Node.js reads cgroup v2 memory limits through libuv starting with Node.js v20.3.0. The v18 release line does not reliably detect cgroup v2 limits, which means it can fall back to reading the host's total memory instead of the container's actual limit — sizing a heap far larger than what the container is allowed to use, and guaranteeing an eventual OOM kill under load.
Even on a version that detects limits correctly, the default heap sizing is generous. A Red Hat engineering analysis of Node.js 20+ in containers found that without --max-old-space-size set, the default old-space heap is roughly 50% of the container's memory, up to a 4 GiB container, after which the maximum heap levels out at about 2 GB. That 50% figure is just the V8 heap — it doesn't include metaspace-equivalent overhead, native buffers, worker threads, or whatever else your process allocates outside the JS heap. A container limited to 512 MiB with no explicit heap flag can still get OOMKilled well before V8 thinks it's close to its own limit, because the container's ceiling and V8's heap ceiling are two different numbers that nobody pinned together.
The fix: pin the container limit and the heap together
The durable fix isn't a bigger memory limit. It's making the container's memory limit and Node's own heap assumption agree, based on what your process actually uses.
- Measure real usage first. Run
docker stats or your metrics stack under representative load and find the steady-state working set, not the idle footprint.
- Set the container limit with headroom. A reasonable starting point is 150-180% of the measured peak, leaving room for non-heap memory (buffers, native modules, worker threads).
- Cap the V8 heap below the container limit, not equal to it:
# Example: 1 GiB container limit, heap capped well under it
docker run -d --name my-service \
--memory=1g --memory-swap=1g \
-e NODE_OPTIONS="--max-old-space-size=700" \
my-registry/my-service:1.4.2
- In Kubernetes, set both the limit and the flag in the same manifest so they can't drift apart during a later resize:
resources:
limits:
memory: "1Gi"
requests:
memory: "1Gi"
env:
- name: NODE_OPTIONS
value: "--max-old-space-size=700"
- On Node 18 or older, don't rely on auto-detection under cgroup v2. Set
--max-old-space-size explicitly, or upgrade to a version in the 20.3.0+ line that reads cgroup v2 correctly.
- Restart and watch the trend, not a single run. A limit that survives one load test can still OOM hours later if there's a real leak — see the next section.
Docker's own resource-constraints documentation covers the --memory, --memory-swap, and --memory-reservation flags in detail if you need soft limits or swap headroom instead of a hard cutoff.
Node.js OOMKilled after hours, not minutes: is it a leak?
If the crash happens quickly under load, it's almost always a sizing problem — fix it with the steps above. If memory climbs steadily over hours or days before the kill, you likely have a real leak: an unbounded cache, a listener that's never removed, or a queue that grows faster than it drains.
Raising --max-old-space-size will not fix a leak — it only delays the inevitable crash and makes it harder to reproduce. Profile first:
# Inside the container
node --max-old-space-size=700 --inspect=0.0.0.0:9229 server.js
# Then connect Chrome DevTools or a heap-snapshot tool remotely
Look for FATAL ERROR: Ineffective mark-compacts near heap limit - JavaScript heap out of memory in the logs before the kill — that's V8 telling you garbage collection couldn't reclaim enough space, which points to a leak rather than a one-time spike.
Quick reference: exit codes you'll see alongside 137
| Exit code | Meaning | First thing to check |
|---|
| 137 | SIGKILL — OOM killer or manual kill | OOMKilled flag, then dmesg/journalctl -k |
| 143 | SIGTERM — graceful stop requested | Who issued the stop; repeated 143s point to a liveness probe |
| 139 | Segmentation fault | Base image and native module compatibility |
| 1 | App started then threw an unhandled error | Application logs |
If your Node process is instead dying from an unguarded async rejection rather than memory pressure, that's a different failure mode — I cover the full pattern in how to fix unhandled promise rejection crashes in Node.js.
How does this interact with a CrashLoopBackOff?
A Kubernetes pod that gets OOMKilled repeatedly will show CrashLoopBackOff as its status once Kubernetes gives up trying to restart it on a fast schedule. The OOMKilled reason and the CrashLoopBackOff state are two layers of the same incident: the OOM kill is the cause, the backoff loop is the symptom Kubernetes shows you first. I walk through diagnosing the loop itself in Kubernetes CrashLoopBackOff: how to fix it fast in 2026 — pair that with the heap-sizing steps here when the root cause turns out to be memory.
Memory pressure also shows up indirectly through database clients: a connection pool that keeps growing because queries are slow or pooled connections never release can push a container over its memory limit just as reliably as a JS heap leak. If your Node service also talks to Postgres through Prisma, check Prisma connection pool timeout: fix it in serverless apps for a related failure pattern.
When to bring in outside help
Right-sizing containers and chasing a heap leak under a release deadline is exactly the kind of work that eats a sprint if you're doing it for the first time. When a client's Node.js service is OOMKilling in production and the fix needs new profiling, a memory audit, or a rebuild of the deployment manifests, that's full backend and DevOps work, not a one-line config change — it's the kind of engagement covered under web app development, where the container setup, the app code, and the deployment pipeline all get looked at together instead of patched in isolation.
Frequently asked questions
What does OOMKilled mean in Docker or Kubernetes?
OOMKilled means the Linux kernel's out-of-memory killer sent SIGKILL to your container's main process because it exceeded its configured memory limit, or because the host itself ran low on memory. Docker and Kubernetes both report it as exit code 137, and you confirm it specifically with docker inspect or kubectl describe pod rather than the exit code alone.
Does increasing --max-old-space-size fix OOMKilled?
Only if the original heap size was genuinely too small for a healthy workload. If the real cause is a memory leak, a bigger heap setting just delays the crash and makes it harder to reproduce — profile the process and look for steadily climbing memory before you touch the flag again.
Why does my container get OOMKilled on Node.js 18 but not on Node.js 20?
Node.js reads cgroup v2 memory limits through libuv starting at v20.3.0; Node.js 18 doesn't reliably detect cgroup v2 limits and can default to sizing its heap off the host's total memory instead of the container's actual limit. Upgrade to a 20.3.0+ line or set --max-old-space-size explicitly on older versions.
Should I set --memory-swap when I set a memory limit?
Only if you want to allow swapping, and most production Node services shouldn't: swapped memory is dramatically slower, and a single-threaded event loop will show it as sudden latency spikes rather than a clean crash. Setting --memory-swap equal to --memory disables swap for that container.
Key takeaways
- OOMKilled and exit code 137 mean the kernel's OOM killer sent SIGKILL because the container hit its memory limit — confirm it with
docker inspect or kubectl describe pod before changing anything.
- Node.js has been container-aware since v12, but only reads cgroup v2 limits reliably from v20.3.0 onward; older runtimes can size a heap against the host's RAM instead of the container's.
- The default V8 heap is roughly 50% of the container's memory up to 4 GiB, capped near 2 GB after that — and it doesn't account for native buffers, workers, or other non-heap memory.
- Fix it by measuring real usage, setting the container limit with honest headroom, and pinning
--max-old-space-size below that limit so the two numbers can't drift apart.
- A crash that appears after hours of steady memory growth is a leak, not a sizing problem — a bigger heap only postpones it.