How to Fix “Free Swap Space: 0 B” Error in Jenkins Docker Containers


2 views
$ docker exec -it jenkins-master free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        1.2G        5.8G         16M        696M        6.2G
Swap:            0B          0B          0B

When your Jenkins master running in Docker reports zero swap space, it's not just a cosmetic issue. Many Java applications, including Jenkins, rely on swap space for proper garbage collection and out-of-memory handling.

The standard approach of creating a swap file on the host machine often doesn't work because:

  • Docker containers don't automatically inherit host swap space
  • Container cgroups may restrict swap usage
  • Some base images disable swap intentionally

Here's how to properly enable swap for your Jenkins container:

# First, create and activate swap on the host (if not existing)
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Then run Jenkins with swap access
docker run -d \
  --memory=2g \
  --memory-swap=4g \
  --memory-swappiness=60 \
  -p 8080:8080 \
  -p 50000:50000 \
  jenkins/jenkins:lts

After applying these changes, check inside the container:

$ docker exec jenkins-container free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        1.2G        5.8G         16M        696M        6.2G
Swap:          2.0G          0B        2.0G

For environments where you can't modify container runtime parameters:

# Create a custom Dockerfile
FROM jenkins/jenkins:lts
USER root
RUN echo 'vm.swappiness=10' >> /etc/sysctl.conf
USER jenkins

This reduces (but doesn't eliminate) swap dependency while maintaining performance.

If you're constrained by environment policies preventing swap usage:

docker run -d \
  --memory=4g \
  --memory-reservation=3g \
  --oom-kill-disable \
  jenkins/jenkins:lts

This configuration provides memory headroom while preventing sudden OOM kills.


html

When running Jenkins in Docker containers, many administrators encounter the warning message free swap space: 0 B. This occurs because Docker containers don't automatically inherit swap space from the host system by default.

Swap space acts as emergency memory when physical RAM is exhausted. Jenkins heavily relies on:

  • Build executors memory allocation
  • Plugin operations
  • Job configuration storage

Without swap space, you might encounter IOException: Not enough space errors during memory-intensive operations.

First, check if swap is enabled in your Docker host:

sudo swapon --show

For container-level verification:

docker exec -it jenkins-container free -h

Modify your Jenkins container deployment with these Docker run parameters:

docker run -d \
  --memory-swap=1g \
  --memory=512m \
  -p 8080:8080 \
  jenkins/jenkins:lts

This allocates 512MB RAM and 1GB total memory (RAM + swap).

For long-term stability, create a dedicated swap file on your host:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make it permanent by adding to /etc/fstab:

/swapfile none swap sw 0 0

For Jenkins running in Kubernetes, configure resource limits:

resources:
  limits:
    memory: "1Gi"
    swap: "2Gi"
  requests:
    memory: "512Mi"
  • Permission issues: Ensure sysctl vm.swappiness=10 is set
  • Docker daemon configuration: Check --memory-swappiness flag
  • Swappiness tuning: Set vm.swappiness = 10 in /etc/sysctl.conf

After implementing swap, monitor performance with:

docker stats jenkins-container

Key metrics to watch:

  • Memory usage vs. swap usage
  • Container OOM events
  • Build execution times