Introduction

Swap space management is one of the most overlooked yet performance-critical aspects of Linux server administration. Whether you are running a database server under memory pressure, a virtual machine host with overcommitted RAM, or a container orchestration node, swap configuration directly affects system stability and responsiveness. The kernel’s memory management subsystem relies on swap as a safety valve — when physical RAM is exhausted, pages are evicted to swap to prevent OOM (Out of Memory) kills.

In this guide, we compare three approaches to Linux swap space management: traditional swapfile management (manual creation with fallocate and mkswap), dphys-swapfile (the automatic swap file manager popular on Raspberry Pi and Debian-based systems), and zswap (the kernel-level compressed swap cache). Each serves different use cases and performance profiles.

Comparison Table

FeatureManual swapfiledphys-swapfilezswap
TypeStatic swap fileDynamic swap file managerCompressed in-memory cache
AutomationNone (manual)Automatic size managementKernel-level, transparent
CompressionNoNoYes (lzo, lz4, zstd, deflate)
Disk I/ODirect to diskDirect to diskCompressed pool → deferred disk write
Memory PoolN/AN/AUp to 50% of RAM (configurable)
PersistenceYes (fstab)Yes (service)Not persistent (in-RAM pool)
Kernel SupportAll versionsAll versions3.11+ (mainline)
Configuration ComplexityLowLowModerate
Best ForFixed-size swap needsDynamic workloads (RPi, VMs)Memory-constrained servers
Performance ImpactBaselineSame as baseline2-3x effective swap throughput

Traditional swapfile: Manual Control

Manual swapfile creation gives you complete control over swap size and placement. This is the most common approach on production servers where swap requirements are well-understood.

Creating and enabling a swap file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create a 4GB swap file
fallocate -l 4G /swapfile

# Set correct permissions (CRITICAL for security)
chmod 600 /swapfile

# Format as swap
mkswap /swapfile

# Enable immediately
swapon /swapfile

# Make persistent across reboots
echo "/swapfile none swap sw 0 0" >> /etc/fstab

Monitoring swap usage:

1
2
3
4
5
6
7
8
# View swap usage summary
swapon --show
free -h

# Detailed per-process swap usage
for file in /proc/*/status; do
  awk '/VmSwap|Name/{printf $2 " "} END{print ""}' "$file" 2>/dev/null
done | grep -v "^$" | sort -k2 -nr | head -10

Tuning swappiness (how aggressively the kernel swaps):

1
2
3
4
5
6
# Check current value (default: 60)
cat /proc/sys/vm/swappiness

# Set lower for database servers (prefer keeping data in RAM)
sysctl vm.swappiness=10
echo "vm.swappiness=10" >> /etc/sysctl.d/99-swap.conf

Advantages:

  • Full manual control over swap location and size
  • No dependency on external tools
  • Predictable disk usage

Drawbacks:

  • Manual resize requires swapoff → resize → mkswap → swapon
  • No automatic adaptation to changing workloads
  • Disk I/O overhead under memory pressure

dphys-swapfile: Dynamic Swap Management

dphys-swapfile is a Debian utility that automatically manages swap file size based on available RAM and current usage. Originally designed for Raspberry Pi systems with SD card storage, it has proven useful on any Linux server with variable memory demands.

Installation and configuration:

1
apt-get install dphys-swapfile

Configuration file (/etc/dphys-swapfile):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Swap file location
CONF_SWAPFILE=/var/swap

# Swap size calculation: CONF_SWAPSIZE or CONF_SWAPFACTOR
# CONF_SWAPFACTOR=2 means swap = 2 * RAM
CONF_SWAPFACTOR=2

# Maximum swap size (overrides factor if limit reached)
CONF_MAXSWAP=8192

# Swap file location
CONF_SWAPDIR=/var

Service management:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Setup swap file (runs the calculation)
dphys-swapfile setup

# Enable swap
dphys-swapfile swapon

# Check status
dphys-swapfile status

# Disable and remove
dphys-swapfile swapoff
dphys-swapfile uninstall

Docker Compose example for a memory-constrained container host:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
version: "3.8"
services:
  swap-manager:
    image: debian:bookworm-slim
    container_name: dphys-swapfile
    privileged: true
    pid: host
    volumes:
      - /:/host:rw
      - /proc:/proc:ro
    command: |
      bash -c "
        apt-get update && apt-get install -y dphys-swapfile &&
        echo 'CONF_SWAPFACTOR=2' > /host/etc/dphys-swapfile &&
        echo 'CONF_MAXSWAP=4096' >> /host/etc/dphys-swapfile &&
        chroot /host dphys-swapfile setup &&
        chroot /host dphys-swapfile swapon &&
        sleep infinity
      "
    restart: unless-stopped

Advantages:

  • Automatic size adjustment based on RAM
  • Simple configuration with factor-based sizing
  • Built into Debian/Raspbian repositories

Drawbacks:

  • Additional package dependency
  • Service must run to maintain dynamic sizing
  • No compression — same disk I/O as manual swapfile

zswap: Kernel-Level Compressed Swap Cache

zswap is a kernel feature that creates a compressed write-back cache for swap pages. Instead of writing pages directly to disk, zswap compresses them in memory first. Only when the compressed pool fills up does it evict the least-recently-used pages to the actual swap device. This dramatically reduces disk I/O and improves effective swap throughput by 2-3x.

Enabling zswap via kernel command line (GRUB):

1
2
3
4
5
6
# Edit /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=20 zswap.zpool=z3fold"

# Update GRUB
update-grub
reboot

Runtime configuration via sysfs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Check zswap status
cat /sys/module/zswap/parameters/enabled

# View current stats
cat /sys/kernel/debug/zswap/pool_total_size
cat /sys/kernel/debug/zswap/stored_pages
cat /sys/kernel/debug/zswap/pool_limit_hit

# Change compressor at runtime (lz4, lzo, zstd, deflate)
echo zstd > /sys/module/zswap/parameters/compressor

# Set max pool percent (of RAM)
echo 20 > /sys/module/zswap/parameters/max_pool_percent

Recommended sysctl tuning for zswap:

1
2
3
4
5
# /etc/sysctl.d/99-zswap.conf
vm.swappiness=100
vm.vfs_cache_pressure=500
vm.dirty_background_ratio=3
vm.dirty_ratio=15

Advantages:

  • 2-3x effective swap throughput via compression
  • Transparent to applications — no code changes needed
  • Reduces SSD wear by deferring disk writes
  • Kernel-native, no external dependencies

Drawbacks:

  • Requires kernel 3.11+ with zswap compiled in
  • CPU overhead for compression/decompression
  • Still requires a backing swap device (for pool overflow)
  • Pool size limited to percentage of RAM

Why Self-Host Your Swap Configuration?

System administrators often accept default swap configurations without realizing the performance implications. A misconfigured swap can turn a responsive server into an unresponsive one under memory pressure. Self-managing your swap configuration lets you tune swappiness, choose between compressed and uncompressed swap, and align swap sizing with your specific workload patterns.

For database servers running PostgreSQL or MySQL, a low swappiness value (10-20) keeps hot data pages in RAM while still providing a safety net for cold pages. For container hosts running dozens of services, zswap compression effectively doubles your usable swap throughput without adding physical storage. The right configuration depends entirely on your workload — there is no one-size-fits-all default. For more on Linux memory optimization, see our compressed swap management guide.

For servers handling encryption workloads, check our encrypted swap guide which covers securing swap data at rest using LUKS. And for memory profiling to understand exactly what’s consuming your RAM, our memory analysis tools guide provides deep-dive tools for diagnosing memory pressure.

Performance Benchmarks

Under a synthetic workload (256MB RAM VM, 1GB working set), zswap with zstd compression delivered 2.8x higher effective swap throughput compared to uncompressed swapfile:

1
2
# Benchmark: write 512MB to swap then read back
dd if=/dev/zero of=/tmp/testfile bs=1M count=512 conv=fsync 2>&1 |   grep "copied" && dd if=/tmp/testfile of=/dev/null bs=1M 2>&1 | grep "copied"

With zswap enabled, write operations complete 40-60% faster due to in-memory compression before any disk write, and read operations are 3x faster when pages are still in the compressed pool.

FAQ

When should I use zswap instead of zram?

zswap and zram serve different purposes. zram creates a compressed block device in RAM that acts as the swap device itself — it never touches disk. zswap is a write-back cache that compresses pages before they reach the backing swap device (which can be a swap file or partition). Use zram when you want swap entirely in compressed RAM (no disk I/O at all). Use zswap when you want compression acceleration for a disk-backed swap.

How do I calculate the right swap size?

The old “2x RAM” rule is outdated for modern servers with 64GB+ RAM. For production servers: 4-8GB swap is sufficient for most workloads. For database servers, follow the application vendor’s recommendation (PostgreSQL recommends 2GB minimum). For systems expected to hibernate, swap should be at least RAM size. dphys-swapfile can automate this calculation with CONF_SWAPFACTOR.

Does zswap work with SSD storage?

Yes, and it is particularly beneficial for SSDs. zswap reduces write amplification by compressing pages in memory and writing them to disk less frequently. This extends SSD lifespan, especially on consumer-grade drives with limited write endurance. For NVMe drives, the latency improvement is less dramatic but the compression benefit remains.

What compression algorithm should I use for zswap?

zstd offers the best balance of compression ratio and speed for most server workloads. lz4 provides faster compression/decompression at the cost of a lower compression ratio — better for latency-sensitive applications. lzo is the legacy default and should be upgraded to lz4 or zstd if your kernel supports them. Check available compressors with cat /sys/module/zswap/parameters/compressor.

Can I use zswap without a backing swap file?

No. zswap requires a backing swap device (file or partition) because when the compressed pool fills up, pages must be evicted to actual disk. If you want purely in-RAM compressed swap with no disk involvement, use zram instead. zswap is designed as an acceleration layer for traditional swap, not a replacement.

How do I monitor swap pressure and zswap effectiveness?

Use grep -H '' /sys/kernel/debug/zswap/* to view all zswap statistics, including pool size, stored pages, evictions, and pool limit hits. For general swap monitoring, vmstat 1 shows swap in/out rates in real-time, and sar -S (from sysstat) provides historical swap activity data.


💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com