Setting up swap space: zram, swapfiles & partitions
Swap space extends physical RAM by letting the kernel page out infrequently accessed anonymous memory. This maintains buffer cache efficiency and prevents out-of-memory kernel panics. Linux supports three swap mechanisms: zram, swapfiles on standard filesystems, and dedicated swap partitions.
In-memory compressed swap: zram
zram creates an in-memory compressed block device. The kernel default compression algorithm is lzo-rle; zstd is a common high-ratio alternative. It provides high-speed paging without disk latency or SSD wear.
Configuring zram with systemd-zram-generator
Create or edit /etc/systemd/zram-generator.conf:
# /etc/systemd/zram-generator.conf
[zram0]
# Allocate zram size equal to physical RAM.
# The default is min(ram / 2, 4096). Override here for more aggressive sizing.
zram-size = ram
# Set compression algorithm
compression-algorithm = zstd
# Assign high priority so zram is used before disk swap
swap-priority = 100Apply and verify the configuration:
# Reload and start service
sudo systemctl daemon-reload
sudo systemctl start systemd-zram-setup@zram0.service
# Verify active zram device
zramctl
swapon --showCreating swapfiles on filesystems
Swapfiles provide flexibility by allocating swap storage directly on an existing filesystem without repartitioning.
Swapfile on Ext4 / XFS Filesystems
Use dd to create the swapfile, which writes sequentially and is supported on all kernels and filesystems. The mkswap manpage recommends dd over fallocate because preallocated files can contain unwritten extents that some kernels reject with swapon: swapfile has holes (notably ext4 on kernels 5.7–5.8, fixed in 5.9).
# 1. Allocate an 8 GB file (dd is portable; fallocate may fail on some kernels)
sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress
# 2. Set strict permissions (required)
sudo chmod 600 /swapfile
# 3. Format as Linux swap
sudo mkswap -L "DISK_SWAP" /swapfile
# 4. Enable swapfile
sudo swapon /swapfileSwapfile on Btrfs Filesystems (btrfs-progs 6.1+)
# 1. Create a dedicated swap subvolume
sudo btrfs subvolume create /swap
# 2. Create the swapfile with automatic NOCOW and preallocation
sudo btrfs filesystem mkswapfile --size 8g /swap/swapfile
# 3. Activate the swapfile
sudo swapon /swap/swapfileBtrfs requires NOCOW for swapfiles because copy-on-write would fragment the swap block device. The Btrfs guide covers NOCOW and subvolume management in more detail.
Configuring swap priorities in /etc/fstab
Assign priority values with pri= in /etc/fstab to ensure fast swap devices are consumed before secondary storage:
# /etc/fstab swap configuration
# zram0 is automatically prioritized at pri=100 by systemd
# Secondary NVMe Swapfile (Used only when zram is saturated)
/swapfile none swap defaults,pri=10 0 0
# Backup Swap on Secondary Drive
/dev/sdb2 none swap defaults,pri=5 0 0If your swap device is on an SSD, enable discard with the discard mount option or fstrim.timer to maintain performance. See TRIM & discard for configuration details.
Sources & references
- systemd-zram-generator — official GitHub repository for systemd-zram-generator, documenting configuration via /etc/systemd/zram-generator.conf
- zramctl(8) — Linux manual page — documents zramctl for setting up and controlling zram devices, including compression algorithms
- swapon(8) — Linux manual page — documents the recommendation to use dd instead of fallocate for swap files, as preallocated files may be rejected by swapon on some kernels
- Btrfs Swapfile Documentation — official Btrfs documentation for swapfile support, documenting the btrfs filesystem mkswapfile command and NOCOW requirements
Frequently asked questions
What is zram swap?
zram creates an in-memory compressed block device for swap. The kernel default compression is lzo-rle; zstd is a common high-ratio alternative. It provides high-speed paging without disk latency or SSD wear.
How do I configure zram with systemd?
Create or edit /etc/systemd/zram-generator.conf with a [zram0] section. Set zram-size, compression-algorithm, and swap-priority, then run sudo systemctl daemon-reload and start the systemd-zram-setup@zram0.service.
How do I create a swapfile on Ext4 or XFS?
Use sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress to allocate an 8 GB file. The mkswap manpage recommends dd over fallocate because preallocated files can be rejected by swapon on some kernels. Set permissions with chmod 600, format with mkswap, and enable with swapon.
How do I create a swapfile on Btrfs?
Use sudo btrfs filesystem mkswapfile --size 8g /swap/swapfile on btrfs-progs 6.1+. This handles NOCOW and preallocation automatically. Then activate with sudo swapon /swap/swapfile.
How do I set swap priority in /etc/fstab?
Use the pri= option. Assign higher values to faster swap: zram at pri=100 (set automatically by systemd), disk swapfiles at pri=10, and backup swap on secondary drives at pri=5.
What is the default zram size with systemd-zram-generator?
The default is min(ram / 2, 4096). You can override this by setting zram-size in /etc/systemd/zram-generator.conf, for example zram-size = ram for more aggressive sizing.