Memory limits and reservations in Docker - Time & Space Complexity
We want to understand how setting memory limits and reservations affects Docker container performance as workload size changes.
How does the system handle memory requests when the workload grows?
Analyze the time complexity of this Docker run command with memory settings.
docker run \
--memory=500m \
--memory-reservation=200m \
myapp:latest
This command runs a container limiting max memory to 500MB and reserving 200MB as guaranteed memory.
Memory limits do not involve loops or repeated code operations inside Docker commands.
- Primary operation: Docker enforces memory limits during container runtime.
- How many times: Memory checks happen continuously as the container runs.
As the container workload grows, memory usage grows up to the limit set.
| Input Size (workload) | Memory Usage Checks |
|---|---|
| Small (10 tasks) | Few checks, memory under reservation |
| Medium (100 tasks) | More frequent checks, memory near reservation |
| Large (1000 tasks) | Many checks, memory approaches limit, possible throttling |
Pattern observation: Memory enforcement happens continuously but scales linearly with workload memory needs.
Time Complexity: O(n)
This means memory checks grow roughly in direct proportion to the workload size.
[X] Wrong: "Memory limits cause the container to slow down exponentially as workload grows."
[OK] Correct: Memory enforcement checks scale linearly, not exponentially, with workload size.
Understanding how resource limits scale with workload is a useful skill for managing container performance in real projects.
What if we removed the memory reservation? How would the time complexity of memory enforcement change?
Practice
memory limit for a Docker container do?Solution
Step 1: Understand memory limit concept
A memory limit restricts the maximum RAM a container can consume to avoid overuse.Step 2: Compare with other options
Memory reservation is the soft minimum, CPU limit is unrelated, disabling memory is invalid.Final Answer:
It sets the maximum memory the container can use. -> Option AQuick Check:
Memory limit = max memory allowed [OK]
- Confusing memory limit with memory reservation
- Thinking memory limit affects CPU
- Assuming memory limit disables memory
Solution
Step 1: Recall Docker memory limit flag
The correct flag is--memoryfollowed by the size with unit, e.g., 500m for 500MB.Step 2: Check syntax correctness
Options A, B, and C use incorrect flag names or units.Final Answer:
docker run --memory=500m myimage -> Option BQuick Check:
Use --memory=500m for 500MB limit [OK]
- Using wrong flag names like --mem-limit
- Using uppercase units like MB instead of m
- Omitting the equal sign
docker run --memory=300m --memory-reservation=200m myapp, what happens if the container tries to use 350MB of memory?Solution
Step 1: Understand memory limit vs reservation
Memory limit (300MB) is the hard max; reservation (200MB) is soft minimum.Step 2: Analyze container memory usage
Using 350MB exceeds the 300MB limit, so Docker kills the container.Final Answer:
The container is killed because it exceeded the 300MB limit. -> Option AQuick Check:
Memory limit exceeded = container killed [OK]
- Confusing reservation with limit
- Thinking container is throttled instead of killed
- Assuming reservation auto-increases
--memory-reservation=400m but your container keeps getting killed when using 350MB. What is the likely cause?Solution
Step 1: Understand memory reservation role
Memory reservation is a soft minimum, it does not kill containers.Step 2: Identify missing memory limit effect
Without a memory limit, Docker may apply a default low limit causing kills.Final Answer:
You forgot to set a memory limit, so default limit is too low. -> Option DQuick Check:
Missing memory limit causes unexpected kills [OK]
- Thinking reservation kills containers
- Ignoring default memory limits
- Assuming reservation enforces max memory
Solution
Step 1: Identify memory reservation and limit roles
Memory reservation sets soft minimum (1GB), memory limit sets hard max (2GB).Step 2: Match options to roles
docker run --memory=2g --memory-reservation=1g mycontainer correctly sets--memory=2g(max) and--memory-reservation=1g(min).Final Answer:
docker run --memory=2g --memory-reservation=1g mycontainer -> Option CQuick Check:
Limit=2g, Reservation=1g for min and max [OK]
- Swapping limit and reservation values
- Setting reservation higher than limit
- Confusing units or flags
Ready to prove it?
Pass the test and this pattern moves unknown
