When Linux servers or desktop workstations experience heavy memory pressure, the kernel begins evicting anonymous memory pages to physical swap space (either a swap partition or a swap file). Because mechanical hard drives and even high-speed NVMe solid-state drives are orders of magnitude slower than system RAM, heavy swapping frequently leads to disk thrashing, severe I/O latency, and unresponsive user interfaces.
The Linux kernel includes an in-tree virtual memory compression feature known as zswap. Unlike zram (which creates an isolated, standalone RAM disk that serves as a swap device), zswap operates as a transparent write-back cache for an existing physical swap device. When the kernel attempts to swap out a page, zswap intercepts the write, compresses the page in memory, and stores it inside a dynamically allocated pool in RAM. Only when the compressed RAM pool fills up does zswap evict the oldest compressed pages to physical disk storage, drastically reducing disk I/O.
Verifying Current zswap Status
Modern Ubuntu releases (such as Ubuntu 22.04 LTS and 24.04 LTS) include the zswap kernel module built in. Check whether zswap is currently enabled on your system:
cat /sys/module/zswap/parameters/enabled
If the command outputs Y, zswap is actively running. If it returns N, zswap is dormant and must be enabled.
You can also inspect the current compression algorithm and memory allocator pool:
cat /sys/module/zswap/parameters/compressor
cat /sys/module/zswap/parameters/zpool
Enabling and Tuning zswap at Runtime
To enable zswap immediately without rebooting the system:
echo 1 | sudo tee /sys/module/zswap/parameters/enabled
1. Selecting a High-Performance Compressor
The default compression algorithm is often lzo or lz4. However, modern kernels support zstd, which offers a superior compression ratio with minimal CPU overhead:
echo zstd | sudo tee /sys/module/zswap/parameters/compressor
2. Choosing the zpool Allocator
The memory pool allocator manages compressed pages inside RAM. Standard allocators include zbud (stores 2 compressed pages per physical page), z3fold (stores up to 3 pages), and zsmalloc (optimised for high-density fragmentation reduction):
echo zsmalloc | sudo tee /sys/module/zswap/parameters/zpool
3. Defining the Maximum RAM Pool Percentage
By default, zswap consumes up to 20% of your total system RAM for its compressed cache. On systems with 16GB or 32GB of RAM, allocating 25% to 30% allows hundreds of thousands of memory pages to stay compressed in RAM without touching physical storage:
# Allocate up to 25% of total physical RAM
echo 25 | sudo tee /sys/module/zswap/parameters/max_pool_percent
Persisting zswap Across System Reboots
Runtime modifications to /sys/module/zswap/ do not survive system reboots. To persist your configuration permanently, add kernel boot parameters to GRUB:
- Open the GRUB default configuration file in a text editor:
sudo nano /etc/default/grub - Locate the line beginning with
GRUB_CMDLINE_LINUX_DEFAULT. - Append the following parameters inside the quotation marks:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.compressor=zstd zswap.zpool=zsmalloc zswap.max_pool_percent=25" - Save the file and exit the editor (in Nano, press Ctrl + O, Enter, then Ctrl + X).
- Update the GRUB bootloader configuration:
sudo update-grub
Monitoring zswap Compression and Efficiency
To observe zswap actively compressing pages under heavy workload, inspect kernel debugfs telemetry:
sudo grep -r . /sys/kernel/debug/zswap/
Key metrics to evaluate include:
stored_pages: The total number of memory pages currently compressed in RAM.pool_total_size: The physical bytes of RAM consumed by the compressed pool.written_back_pages: Pages that had to be decompressed and written to physical swap disk because the pool was saturated.reject_compress_poor: Pages rejected from zswap because they could not be compressed efficiently.
Enabling zswap delivers significant responsiveness improvements on memory-constrained servers, build agents, and developer desktop environments.