Understanding ZRAM: Linux Memory Compression and Swap Optimization
This article explains the ZRAM technology in Linux, covering its principles, configuration steps, kernel integration, performance optimizations, and practical use cases for improving memory utilization on embedded devices, Android, and legacy PCs.
1. Introduction to ZRAM
Modern digital devices often suffer from memory shortage, leading to sluggish performance or crashes. ZRAM is a Linux kernel feature that compresses rarely used memory pages and stores them in a virtual RAM block device, reducing the need for disk‑based swap and improving overall responsiveness.
1.1 Overview
When the system runs out of RAM, the swap mechanism moves inactive pages to a swap area. ZRAM creates an in‑memory block device that holds these pages in compressed form, allowing the kernel to reclaim memory without the I/O penalty of traditional disk swap.
Compressed pages occupy less space; for example, a 50 MB page compressed at a 0.4 ratio only uses 20 MB in the ZRAM device, effectively expanding usable memory.
2. How ZRAM Works
ZRAM builds on the swap subsystem. Instead of writing pages to a physical disk, the kernel writes them to the ZRAM block device, which compresses the data before storage. Because the device resides in RAM, no disk I/O occurs, avoiding the performance hit associated with conventional swap.
2.1 ZRAM Workflow
The swap subsystem selects rarely used memory pages.
These pages are written to the ZRAM block device, where they are compressed.
The compressed data is stored in a memory pool and indexed by a table.
When a process accesses a compressed page, the data is decompressed and mapped back into RAM.
2.2 Configuration Steps
(1) Kernel configuration
Device Drivers -> Staging drivers (STAGING [=y])For kernels 3.15 and newer:
Device Drivers -> [*] Block devices -> Compressed RAM block device supportKey kernel options:
CONFIG_RESOURCE_COUNTERS=y
CONFIG_MEMCG=y
CONFIG_MEMCG_SWAP=y
CONFIG_MEMCG_SWAP_ENABLED=y
CONFIG_MEMCG_KMEM=y
CONFIG_ZRAM=y
CONFIG_TOI_ZRAM_SUPPORT=y
CONFIG_ZRAM_DEBUG=y(2) Set number of ZRAM devices
modprobe zram num_devices=4(3) Set maximum compression streams (kernel 3.15+)
echo 3 > /sys/block/zram0/max_comp_streams(4) Choose compression algorithm
cat /sys/block/zram0/comp_algorithm
# output: lzo [lz4]
echo lzo > /sys/block/zram0/comp_algorithm(5) Set ZRAM size (10‑25% of total RAM is typical)
echo $((512*1024*1024)) > /sys/block/zram0/disksize # 512 MiB(6) Initialise swap on the ZRAM device
mkswap /dev/zram0
swapon /dev/zram03. Implementation Details
The ZRAM driver resides in drivers/block/zram/zram_drv.c . When swap writes a page, the kernel calls zram_make_request() , which eventually invokes zram_bvec_write() to compress the page and store it in the ZRAM memory pool.
static int
zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, int offset)
{
// 1. Get the page to compress
page = bvec->bv_page;
user_mem = kmap_atomic(page);
// 2. Compress the memory
ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
// 3. Allocate a block for the compressed data
handle = zs_malloc(meta->mem_pool, clen);
cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
// 4. Copy compressed data
memcpy(cmem, src, clen);
// 5. Record the handle in the table
meta->table[index].handle = handle;
return ret;
}The driver uses the zsmalloc allocator, which packs multiple compressed objects into composite pages, allowing efficient memory use even under pressure.
3.1 Performance Optimisations
Early kernels used a single compression stream, which could become a bottleneck. Starting with kernel 4.19, each CPU gets its own compression stream, enabling parallel compression and significantly improving throughput on multi‑core systems.
4. Benefits and Use Cases
4.1 Advantages
Improved system performance: Compression occurs in RAM, avoiding slow disk I/O.
Higher memory utilisation: Compressed pages allow more applications to reside in limited RAM.
Extended storage lifespan: Reduces write traffic to SSDs or flash storage.
4.2 Typical Scenarios
Embedded devices (e.g., smart cameras) where RAM and flash are scarce.
Android smartphones (default since Android 4.4) to keep UI responsive under heavy multitasking.
Legacy PCs with 1‑2 GB RAM that cannot be upgraded economically.
In all these cases, enabling ZRAM provides a low‑cost, kernel‑level solution to mitigate memory pressure without sacrificing performance.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
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.