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.
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
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'
- 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
- .