Skip to the page

Kubernetes: shipping a service · mid

Requests, limits and OOMKilled

Requests decide scheduling, limits decide enforcement. A container over its memory limit is killed by the kernel, and for Java the heap is only a part of the memory the process uses.

The notes

What you must know

  • Requests: what the scheduler reserves for the pod on a node. CPU limits throttle the container. Memory limits kill it: going over the limit means the kernel kills the container (OOMKilled, exit code 137).
  • OOMKilled is not a Java OutOfMemoryError. The error would be in the logs. OOMKilled shows in kubectl describe pod under Last State and in the working-set memory graph.
  • Java memory is heap plus metaspace, thread stacks (about 1 MB per thread), code cache, direct buffers (Netty, NIO), GC structures and native libraries. A 900 MB heap in a 1 GiB limit leaves almost nothing for the rest.
  • Pattern: dies every evening at peak. More request threads, large responses built in memory, growing direct buffers and a heap that fills to its maximum push the total over the limit. This is not a leak.
  • Fix: set the heap as a percentage of the container limit (for example -XX:MaxRAMPercentage) with real headroom, limit threads, stream large responses, and measure at peak.
  • Node and Python: set the old-space or worker limits to fit the container limit as well.
  • Setting requests equal to limits for memory gives predictable behaviour. CPU limits can throttle latency-sensitive services, so many teams set CPU requests and no CPU limit. Know the trade-off.
  • Requests are also what HPA percentages are based on.

The red pen

Common traps: mistake → fix

  • Mistake: Memory limit equals the Java heap size

    Fix: The process uses more than the heap. Leave headroom.

  • Mistake: OOMKilled means a memory leak

    Fix: It can be normal peak use over a limit that is too tight.

  • Mistake: CPU limits are free protection

    Fix: They cause throttling and higher latency when the process needs a burst.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between a request and a limit?
  2. What happens when a container goes over its memory limit and over its CPU limit?
  3. How do you tell OOMKilled from a Java OutOfMemoryError?
  4. Why is a 900 MB heap in a 1 GiB container risky?
  5. A pod is OOMKilled every evening but memory looks fine all day. How do you investigate?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview