Q: Your Redis cache containers and Java JVM microservices run on multi-tenant Docker hosts with host swap enabled. When host memory pressure occurs, the Linux kernel aggressively swaps out JVM heap pages to host swap disk, degrading API response times from 5ms to 1,200ms due to disk paging thrashing. Meanwhile, engineers are confused by how `--memory-swap` interacts with `--memory`. You must formulate the mathematical relationship of Docker memory flags, disable swap for latency-critical caches, and tune swappiness.
Configure and calculate Docker container memory ceilings: --memory, --memory-swap, and --memory-swappiness. Prevent severe disk thrashing caused by excessive swapping and avoid sudden OOM kills.
--memory, --memory-swap, and --memory-swappiness. Prevent severe disk thrashing caused by excessive swapping and avoid sudden OOM kills."
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: Configure and calculate Docker container memory ceilings: --memory, --memory-swap, and --memory-swappiness. Prevent severe disk thrashing caused by excessive swapping and avoid sudden OOM kills.
Deconstruct the Mathematical Relationship of Docker Memory Flags
Understand the precise calculation: --memory-swap represents total memory (RAM + SWAP combined), NOT swap alone. If --memory=1g and --memory-swap=3g, the container has 1GB of physical RAM and 2GB of swap space.
<!-- Docker Memory & Swap Formula -->
Total Container Memory Ceiling = --memory-swap
Physical RAM Allocation = --memory
Actual Permitted Swap Space = --memory-swap MINUS --memory
Rule Examples:
- Set RAM to 2GB, Swap to 1GB: --memory=2g --memory-swap=3g
- Disable swap completely: --memory=2g --memory-swap=2g (RAM == Total)
- Allow unlimited swap (Host max): --memory=2g --memory-swap=-1
Completely Disable Swap for Latency-Critical Workloads
For latency-critical in-memory databases (Redis, Memcached) or garbage-collected runtimes (Java/Go), swapping degrades throughput catastrophically. Set --memory-swap equal to --memory to strictly prohibit swap paging.
# Prohibit swap paging for Redis
docker run -d \
--name redis-cache \
--memory="4g" \
--memory-swap="4g" \
redis:7-alpine
Tune Container Swappiness (--memory-swappiness)
Adjust the kernel's aggressiveness when swapping page cache versus anonymous memory using --memory-swappiness (0 to 100). A value of 0 instructs the kernel to avoid swapping process memory unless absolutely necessary.
# Set swappiness to 0 to prioritize file cache reclamation over anonymous heap paging
docker run -d \
--name java-api \
--memory="2g" \
--memory-swap="3g" \
--memory-swappiness=0 \
openjdk:21-slim
Verify Cgroup Memory and Swap Limits in the Host Kernel
Inspect the container's cgroup controllers to confirm limits are applied correctly by the kernel.
# In cgroups v2:
CONTAINER_ID=$(docker inspect redis-cache --format '{{.Id}}')
cat /sys/fs/cgroup/docker/${CONTAINER_ID}/memory.max
cat /sys/fs/cgroup/docker/${CONTAINER_ID}/memory.swap.max
# Inspect memory statistics and major page faults (disk paging)
cat /sys/fs/cgroup/docker/${CONTAINER_ID}/memory.stat | grep -E 'anon|file|pgpgin|pgpgout'
--memory-swap represents total memory (RAM plus Swap), not standalone swap. Setting --memory-swap equal to --memory prohibits swapping, preventing disk thrashing and maintaining microsecond response times for databases and garbage-collected runtimes."
- W
- e
- s
- o
- l
- v
- e
- d
- s
- e
- v
- e
- r
- e
- 1
- ,
- 0
- 0
- 0
- m
- s
- l
- a
- t
- e
- n
- c
- y
- s
- p
- i
- k
- e
- s
- i
- n
- o
- u
- r
- c
- o
- n
- t
- a
- i
- n
- e
- r
- i
- z
- e
- d
- J
- a
- v
- a
- a
- n
- d
- R
- e
- d
- i
- s
- s
- e
- r
- v
- i
- c
- e
- s
- b
- y
- c
- o
- r
- r
- e
- c
- t
- i
- n
- g
- t
- h
- e
- i
- r
- m
- e
- m
- o
- r
- y
- f
- l
- a
- g
- s
- .
- B
- e
- c
- a
- u
- s
- e
- `
- -
- -
- m
- e
- m
- o
- r
- y
- -
- s
- w
- a
- p
- `
- d
- e
- f
- i
- n
- e
- s
- t
- o
- t
- a
- l
- R
- A
- M
- p
- l
- u
- s
- s
- w
- a
- p
- ,
- s
- e
- t
- t
- i
- n
- g
- `
- -
- -
- m
- e
- m
- o
- r
- y
- -
- s
- w
- a
- p
- `
- e
- q
- u
- a
- l
- t
- o
- `
- -
- -
- m
- e
- m
- o
- r
- y
- `
- a
- l
- o
- n
- g
- s
- i
- d
- e
- `
- -
- -
- m
- e
- m
- o
- r
- y
- -
- s
- w
- a
- p
- p
- i
- n
- e
- s
- s
- =
- 0
- `
- c
- o
- m
- p
- l
- e
- t
- e
- l
- y
- p
- r
- e
- v
- e
- n
- t
- e
- d
- t
- h
- e
- k
- e
- r
- n
- e
- l
- f
- r
- o
- m
- p
- a
- g
- i
- n
- g
- a
- p
- p
- l
- i
- c
- a
- t
- i
- o
- n
- h
- e
- a
- p
- s
- t
- o
- d
- i
- s
- k
- ,
- r
- e
- s
- t
- o
- r
- i
- n
- g
- s
- t
- e
- a
- d
- y
- -
- s
- t
- a
- t
- e
- 5
- m
- s
- l
- a
- t
- e
- n
- c
- y
- .
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Structuring Dockerfile instructions inefficiently by copying app source before package manifests when configuring Mastering Container Memory
Copying code before package manifests (
package.json,go.mod,requirements.txt) invalidates the build cache on every commit, forcing slow full reinstalls. -
Neglecting the PID 1 process reap problem and omitting proper SIGTERM signal forwarding when configuring Mastering Container Memory
If an application runs as PID 1 without an init system (like
tini), it fails to reap zombie child processes and ignoresSIGTERMsignals, causing unclean 10s timeouts. - Assembling single-stage images containing compilers, build SDKs, and header files in production when configuring Mastering Container Memory Single-stage builds bloat image size by gigabytes and expose extensive package vulnerabilities; multi-stage builds isolate artifacts into minimal distroless images.
- Confusing container port forwarding on bridge networks with unisolated host networking mode when configuring Mastering Container Memory Bridge networking utilizes iptables NAT translations which add latency overhead, whereas host networking bypasses the network namespace entirely.