Fundamentals 12 min read

Overview of Linux Memory Compression Technologies: zSwap, zRAM, and zCache

Linux reduces RAM pressure through three main compression mechanisms—zSwap, which caches compressed pages before writing to swap; zRAM, a RAM‑backed compressed block device; and zCache, a file‑page compressor—each paired with specialized allocators (zsmalloc, zbud, z3fold) and configurable algorithms, offering trade‑offs in speed, ratio, CPU load, and fragmentation.

OPPO Kernel Craftsman
OPPO Kernel Craftsman
OPPO Kernel Craftsman
Overview of Linux Memory Compression Technologies: zSwap, zRAM, and zCache

Memory compression is a technique used to reduce RAM pressure by compressing pages before they are swapped out, thereby decreasing I/O latency and improving overall system performance, especially on devices where RAM is limited.

The Linux kernel provides three mainstream memory‑compression mechanisms: zSwap , zRAM , and zCache .

zSwap acts as a cache layer between RAM and the swap device. When memory needs to be swapped, pages are first compressed and stored in zSwap, which grows on demand. Once zSwap is full, the oldest pages (according to LRU) are decompressed and written to the backing swap device. Drawbacks include the requirement for an LRU‑aware memory allocator and potential performance penalties when the full‑swap‑out mode is enabled.

zRAM creates a compressed block device in RAM. It uses a special allocator (zsmalloc) to store compressed anonymous pages, avoiding writes to physical storage. This is widely used on mobile devices because compression/decompression is much faster than flash I/O. Issues include configuration complexity, CPU overhead in low‑memory scenarios, and possible fragmentation when the allocator cannot move pages.

zCache (proposed by Oracle) compresses file pages and sits between memory and block devices, similar to zSwap but targeting file‑page caches rather than anonymous pages. Its limitations involve handling already‑compressed files, lower compression ratios with the default zbud allocator, and fragmentation concerns.

The kernel also provides three memory allocators tailored for compression: zsmalloc (designed for zRAM, aims to work well under severe fragmentation), zbud (stores two objects per page, supports LRU but allocates immovable memory), and z3fold (stores three objects per page, also LRU‑capable but immovable). Choosing the right allocator depends on the compression technology and workload.

A comparative analysis shows the most suitable pairings: zSwap with zsmalloc/zbud/z3fold, zRAM with zsmalloc, and zCache with zsmalloc or zbud, each balancing compression ratio, fragmentation, and CPU usage.

The zRAM architecture consists of three parts: a data‑flow layer that handles serial/parallel compression and decompression, a pluggable compression algorithm (e.g., LZ4, ZSTD), and the zram driver that presents a RAM‑backed block device to the kernel.

Compression is triggered mainly by two scenarios: (1) the kswapd daemon when low‑watermark thresholds are crossed, and (2) direct reclaim during memory allocation slow‑paths. The article includes flow diagrams illustrating how pages are shrunk, compressed, and later decompressed on access.

Common compression algorithms such as LZ4, LZ0, and ZSTD are compared, showing trade‑offs between compression ratio and speed.

Typical steps to enable and configure zRAM on a Linux system are:

echo lz4 > /sys/block/zram0/comp_algorithm

echo 2147483648 > /sys/block/zram0/disksize # 2 GB

mkswap /dev/zram0

swapon /dev/zram0

The swappiness sysctl (0‑100) controls how aggressively the kernel swaps anonymous pages; a higher value increases swap usage, while 0 disables anonymous page swapping.

Key metrics for monitoring zRAM include total and free swap size (SwapTotal/SwapFree), the configured disk size (/sys/block/zram0/disksize), compression ratio (orig_data_size vs. compr_data_size in /sys/block/zram*/mm_stat), and swap‑in/out counters (pswpin/pswpout in /proc/vmstat).

Optimization recommendations cover proper sizing of zRAM (avoiding overly large configurations that increase fragmentation), selecting efficient compression algorithms to reduce CPU load, and mitigating fragmentation by using newer kernels where zsmalloc supports movable allocations.

performanceKernellinuxzRAMMemory CompressionzCachezSwap
OPPO Kernel Craftsman
Written by

OPPO Kernel Craftsman

Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.