Shared Memory

Difficulty: Advanced

Question

How do you increase the shared memory (/dev/shm) available to a container, and which workloads need it?

Answer

Pass --shm-size to docker run, for example docker run --shm-size=1g ..., or set shm_size: "1gb" in Docker Compose. Docker gives every container its own /dev/shm tmpfs of only 64 MB by default, which is too small for headless browsers, PostgreSQL parallel queries and multi-worker PyTorch data loading.

What /dev/shm is

/dev/shm is a memory-backed tmpfs that processes use for POSIX shared memory (shm_open) and fast inter-process communication. Several processes map the same region and read each other's data without copying through pipes or sockets. Because each container has its own IPC namespace, it also gets its own /dev/shm, and Docker caps it at 64 MB unless told otherwise.

Workloads that hit the 64 MB wall

WorkloadSymptomTypical fix
Chrome or Chromium (Puppeteer, Playwright, Selenium)Tabs crash, "Aw, Snap!", page.goto hangs on heavy pages--shm-size=1g or launch Chrome with --disable-dev-shm-usage
PostgreSQL parallel queries"could not resize shared memory segment ... No space left on device"--shm-size=256m or more, depending on workers and work_mem
PyTorch DataLoader with num_workers > 0"DataLoader worker killed by signal: Bus error"--shm-size=8g or --ipc=host
Oracle, SAP and some JVM IPC setupsStartup failures about shared memoryVendor recommended size

Think of a Flipkart-style scraping or PDF-invoice service running Playwright: it works in local tests with small pages and then crashes in production on a heavy product page. Shared memory is often the cause.

Other ways to provide shared memory

  • --ipc=host shares the host's IPC namespace and its full /dev/shm. It removes the limit but also the isolation, so a container can see other processes' shared memory segments. Acceptable for a dedicated GPU training box, not for multi-tenant hosts.
  • --ipc=shareable / --ipc=container:<name> lets two containers share one IPC namespace intentionally.
  • Kubernetes has no shm-size field. Mount an emptyDir with medium: Memory (and a sizeLimit) at /dev/shm.

Gotchas

  • It still uses RAM. tmpfs pages are memory. Data written to /dev/shm is generally charged to the container's memory cgroup, so a large shm combined with a tight --memory limit can end in an OOM kill.
  • Size is set at creation. You cannot change shm-size with docker update; recreate the container.
  • Units. --shm-size accepts b, k, m, g. Compose accepts strings like "2gb".
  • Check before tuning. docker exec <id> df -h /dev/shm shows the current size and usage.
Where /dev/shm sits in a container. Each container gets a private shared memory tmpfs inside its IPC namespace, backed by host RAM.
  1. Processes in the container: Chrome renderers, Postgres workers, DataLoader workers.
  2. /dev/shm tmpfs (64 MB default): Shared memory segments they map and exchange data through.
  3. Container IPC namespace: Isolates shm from other containers unless --ipc says otherwise.
  4. Memory cgroup and host RAM: tmpfs pages consume real memory on the host.

Code examples

Check and raise shared memory

# Default size
docker run --rm alpine df -h /dev/shm
# shm   64.0M   0   64.0M   0% /dev/shm

# Headless Chrome / Playwright worker with 1 GB of shm
docker run -d --name pdf-renderer --shm-size=1g --memory=2g myorg/pdf-renderer:1.3

# PostgreSQL with room for parallel query workers
docker run -d --name pg --shm-size=512m -e POSTGRES_PASSWORD=dev postgres:16

docker exec pg df -h /dev/shm

The memory limit is set higher than shm-size because shared memory pages count as real memory usage.

Compose and Kubernetes equivalents

# compose.yaml
services:
  trainer:
    image: pytorch/pytorch:latest
    shm_size: "8gb"
---
# Kubernetes Pod snippet: no shm-size field, mount a memory emptyDir instead
spec:
  containers:
    - name: renderer
      image: myorg/pdf-renderer:1.3
      volumeMounts:
        - name: dshm
          mountPath: /dev/shm
  volumes:
    - name: dshm
      emptyDir:
        medium: Memory
        sizeLimit: 1Gi

Compose maps shm_size straight to --shm-size. In Kubernetes the memory-backed emptyDir replaces the default /dev/shm and counts toward the pod's memory.

Key points

  • Docker's default /dev/shm is 64 MB per container
  • Raise it with --shm-size (docker run) or shm_size (Compose)
  • Headless Chrome, PostgreSQL parallel queries and PyTorch DataLoader workers commonly need more
  • --ipc=host removes the limit but also IPC isolation
  • shm is RAM: size the memory limit accordingly; it cannot be changed with docker update

Common follow-up questions

  • Why does Chrome's --disable-dev-shm-usage flag help, and what does it cost?
  • What is an IPC namespace?
  • How do you give a Kubernetes pod more shared memory?
  • Why might raising shm-size lead to an OOM kill?

Interview tips

  • Quote the 64 MB default and a concrete symptom such as a Bus error or Chrome crash; it shows you have debugged this
  • Mention the Kubernetes emptyDir workaround to show you can carry the fix to orchestration

Concepts covered

/dev/shm, --shm-size, Shared memory (tmpfs), Headless Chrome, PostgreSQL parallel query, PyTorch DataLoader

More Docker & Containers questions