Course contentsShow
← Cloud and Infrastructure
Lesson 1335 of 2,98927. Kubernetes Scheduling and ResourcesFree lesson

Setting Memory Limits

Define memory limits to cap maximum memory usage and understand Out-Of-Memory (OOM) behavior.

Setting Memory Limits

What you'll learn: How to cap maximum memory usage for containers and prevent runaway memory consumption.

What Are Memory Limits?

While memory requests tell Kubernetes how much memory your container expects to use, memory limits set a hard ceiling on how much memory your container is allowed to consume. Think of it like a water tank with a fixed capacity—once it's full, no more water can flow in.

A memory limit prevents a single misbehaving container from monopolizing all available RAM on a node and starving other workloads.

Defining Memory Limits

Memory limits are specified in your Pod manifest using the same units you learned for memory requests (Mi, Gi, etc.):

apiVersion: v1
kind: Pod
metadata:
  name: limited-app
spec:
  containers:
  - name: app
    image: myapp:1.0
    resources:
      requests:
        memory: "256Mi"
      limits:
        memory: "512Mi"

In this example, the container requests 256Mi but cannot exceed 512Mi.

Out-Of-Memory (OOM) Behavior

Here's what happens when a container tries to exceed its memory limit:

  1. Hard Stop: The kernel immediately kills the container process
  2. OOMKilled Status: Kubernetes marks the container with an OOMKilled exit reason
  3. Restart Policy: The Pod's restart policy determines if the container restarts

Unlike CPU (which gets throttled), memory cannot be "borrowed back"—exceeding the limit triggers immediate termination.

Best Practices

  • Set realistic limits based on actual application behavior
  • Monitor OOMKilled events to detect undersized limits
  • Leave headroom between requests and limits for memory spikes
  • Test under load to understand your application's peak memory usage

Key Takeaway: Memory limits set a hard cap on container memory usage; exceeding the limit triggers an Out-Of-Memory kill, making proper sizing critical for application stability.