The zram module creates compressed block devices in RAM. These devices compress data automatically when you write to them. This compression happens in real-time, storing data in memory rather than on disk. Linux systems use zram primarily for swap space and temporary file storage. The module has been part of the mainline kernel since version 3.14. You can configure multiple zram devices simultaneously. Each device operates independently with its own compression algorithm and capacity settings. The result is faster memory operations with significant space savings.
How Zram Works
Zram generates block devices at paths like /dev/zram0 and /dev/zram1. When you write data, the module compresses it before storage. The compressed content remains in system memory.
This approach delivers quick input and output operations. You can use these devices for swap partitions, temporary directories, or cache storage. The compression typically achieves a 2:1 ratio, effectively doubling your usable memory.
Performance statistics are available through sysfs nodes at /sys/block/zram<id>/. You can monitor compression ratios, memory usage, and other metrics in real-time.
Setting Up Zram
Loading the Kernel Module
Start by loading the zram module with modprobe. You can specify how many devices to create.
Run modprobe zram num_devices=4 to create four block devices. Without parameters, the command creates one device by default.
Selecting a Compression Algorithm
Zram supports multiple compression algorithms. View available options by reading the comp_algorithm file.
Execute cat /sys/block/zram0/comp_algorithm to see supported algorithms. The currently selected algorithm appears in brackets.
Select your preferred algorithm before initialization. Write the algorithm name to the comp_algorithm file. For example, echo lzo > /sys/block/zram0/comp_algorithm selects the lzo compression method. After device initialization, you cannot change the compression algorithm.
Configuring Device Size
Set your device capacity through the disksize attribute. Memory suffixes simplify specification.
Write echo 512M > /sys/block/zram0/disksize for a 512 megabyte device. Use K for kilobytes or G for gigabytes. Experts recommend keeping capacity below twice your physical RAM amount.
Activating the Device
Format your zram device based on intended use. For swap space, run mkswap on the device path.
Execute mkswap /dev/zram0 to prepare the device for swap. Then activate it with swapon /dev/zram0.
For filesystem mounting, format with mkfs.ext4 /dev/zram1. Then mount it with mount /dev/zram1 /tmp.
Monitoring Zram Performance
The zram module exports detailed metrics through sysfs files. The mm_stat file reveals critical memory statistics.
Check orig_data_size for uncompressed bytes. The compr_data_size shows compressed storage size. The mem_used_total indicates total memory allocation.
Additional metrics include same_pages for identical content detection. These statistics help you evaluate compression effectiveness and memory savings.
Advanced Zram Features
Idle Page Tracking
Mark allocated but unused pages as idle. Write echo all > /sys/block/zram0/idle to flag these pages. This helps identify candidates for writeback or recompression.
External Storage Writeback
Zram can offload idle or incompressible pages to backing storage. Configure a backing device before setting capacity. Execute echo /dev/sda5 > /sys/block/zram0/backing_dev to enable this feature.
Secondary Compression
The module supports multiple compression algorithms simultaneously. Primary compression runs first. Secondary algorithms can recompress pages later for better ratios.
Managing Device Limits
Restrict maximum memory consumption through the mem_limit attribute. Write values like echo 512M > /sys/block/zram0/mem_limit to set limits. Setting this to zero removes restrictions.
Resetting Devices
Deactivate devices before cleanup. For swap devices, run swapoff /dev/zram0. For mounted filesystems, use umount /dev/zram1.
Reset devices by writing any positive value to the reset file. Execute echo 1 > /sys/block/zram0/reset to release allocated memory and clear settings.
FAQs
What is zram used for on Linux systems?
Zram creates compressed RAM-based block devices for swap space and temporary storage. It compresses data in memory to increase available space without using disk storage. Common uses include swap partitions and temporary file directories.
How much memory can zram save?
Zram typically achieves a 2:1 compression ratio, effectively doubling usable memory. Actual savings depend on data compressibility and the chosen algorithm. You can monitor compression effectiveness through sysfs statistics files.
Can you change zram compression algorithm after initialization?
No, you cannot change the compression algorithm after device initialization. Select your algorithm by writing to /sys/block/zram0/comp_algorithm before setting the disksize. To change algorithms, reset the device and reconfigure it.
Does zram work on systems with limited RAM?
Yes, zram works particularly well on memory-limited systems. It provides additional swap space without disk I/O overhead. Systems with less than 2GB RAM benefit most from zram compression.
How do you monitor zram performance?
Monitor zram through sysfs files at /sys/block/zram0/. Check mm_stat for compression ratios and memory usage. The orig_data_size and compr_data_size values show compression effectiveness in real-time.