Boost Linux Performance with zSwap, zRAM, and Zstandard Compression
This article explains how Linux memory compression techniques such as zSwap, zRAM, and the Zstandard algorithm reduce I/O pressure, extend flash lifespan, and improve overall system performance, while also covering their drawbacks and step‑by‑step activation procedures.
Why Memory Compression Matters
Compressing memory reduces space usage; I/O operations are far slower than RAM access, so frequent I/O not only shortens flash lifespan but also degrades system performance. Memory compression smooths I/O transitions, mitigating performance loss caused by memory pressure.
Introducing zSwap
zSwap is a Linux kernel feature that provides a compressed write‑back cache for swap pages. Instead of moving pages directly to a swap device, zSwap compresses them and stores them in a dynamically allocated RAM pool, postponing or even avoiding writes to the swap device and thus reducing Linux I/O at the cost of extra CPU cycles for compression.
By using zSwap, Linux can utilize RAM more efficiently, effectively increasing available memory without a proportional increase in CPU usage. The feature exists in the kernel but is disabled by default; it must be enabled via configuration.
Main Memory‑Compression Technologies
zSwap
zRAM
zCache
How zSwap Works
zSwap acts as a cache layer between memory and flash. When a page needs to be swapped out to disk, it is first compressed and placed in zSwap, which grows on demand. Once a certain threshold is reached, pages are evicted in LRU order (provided the memory allocator supports LRU), decompressed, and written to the swap device.
Drawbacks include the requirement for an LRU‑capable allocator and similarity to zRAM when the backing store is fully swapped.
zRAM
zRAM creates a compressed RAM block device. It never writes to an actual block device; compressed pages remain in RAM, offering much faster access than I/O and making it popular on mobile devices. Issues include configuring the size, increased CPU usage by the kswapd process in low‑memory scenarios, and potential fragmentation from large zRAM allocations.
zCache
Proposed by Oracle, zCache compresses file pages and sits between memory and block devices, similar to zSwap but targeting file‑page compression. In contrast, zSwap and zRAM compress anonymous pages.
zstd Compression Algorithm
Zstandard (zstd) is a real‑time compression algorithm offering high compression ratios with a wide range of speed/ratio trade‑offs. It includes a fast decoder and supports dictionary compression for small data sets. The library is released under the BSD license.
Enabling zSwap
# edit grub as root
sudo vim /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.compressor=zstd zswap.zpool=z3fold"
# update grub
sudo update-grub
# install zstd and Z3fold
sudo apt install checkinstall build-essential
sudo apt install zstd # not the latest version
# edit the modules file
sudo vim /etc/initramfs-tools/modules
# add the following lines
zstd
zstd_compress
z3fold
# update initramfs
sudo update-initramfs -u -k all
# restart and verify
cat /sys/module/zswap/parameters/enabled # should output Y
sudo dmesg | grep -i zswap:
[ 1.059997] zswap: loaded using pool zstd/z3foldReferences
Improving Elementary OS Performance using ZSWAP, zstd and z3fold
Linux 内存压缩浅析之原理
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
