首页 公告 项目 RSS

⬇️⬇️⬇️ 欢迎关注我的 telegram 频道和 twitter ⬇️⬇️⬇️


联系方式: Twitter Github Email Telegram

Introduction to zram

September 17, 2025 本文有 411 个字 需要花费 2 分钟阅读

Introduction

zram is a native Linux memory compression module. It creates a virtual block device in memory, with all written data instantly compressed and stored. The most common use is as a swap device: it is more than ten times faster than traditional disk swap and significantly reduces disk wear. zram is often used on low-spec devices such as Raspberry Pi and routers, but even on machines with ample memory, enabling zram can boost performance and reduce out-of-memory issues. In short, zram swap is an enhancement that’s “harmless to enable, extremely useful in emergencies.”

Configuration

I use NixOS and Ubuntu most often, so here’s how to configure on both systems.

For NixOS, simply add to /etc/nixos/configuration.nix:

zramSwap.enable = true;

This works out of the box—by default, it uses half your total RAM. You can further tweak with these parameters:

  • zramSwap.priority: sets swap priority.
  • zramSwap.memoryMax: sets max memory for the zram device (i.e., max compressed block device size).
  • zramSwap.algorithm: specifies compression algorithm for zram.
  • zramSwap.swapDevices: allows declaring multiple zram swap devices within configuration.
  • zramSwap.memoryPercent: automatically sets zram swap size based on RAM percentage (default 50%).
  • zramSwap.writebackDevice: experimental; supports “writeback” so excess swap data gets compressed to zram, with overflow written to disk (dual strategy).

After saving, apply with:

nixos-rebuild switch

This activates zram swap.

Then verify with:

swapon --show

For Ubuntu, simply install:

sudo apt install zram-tools

Then configure parameters, similar to NixOS. Edit /etc/default/zramswap:

# Compression algorithm selection
# speed: lz4 > zstd > lzo
# compression: zstd > lzo > lz4
# Not all options available in latest kernels
# See /sys/block/zram0/comp_algorithm for your kernel’s options
ALGO=lz4

# Amount of RAM to use for zram (% of total memory)
# Overrides SIZE below
PERCENT=50

# Static RAM size for zram devices (MiB)
SIZE=4096

# Swap device priority (see swapon(2)). Higher number = higher priority.
PRIORITY=100
  • ALGO: Compression algorithm used by zram.
  • PERCENT: % of total RAM for zram.
  • SIZE: Fixed swap size in MiB.
  • PRIORITY: Priority value for this swap device. Higher priority means the system prefers this device (e.g., set zram swap to 100, much higher than disk swap).

Restart the service to apply:

systemctl restart zramswap.service

Check results with:

swapon --show

Compression Algorithms

zram supports many compression algorithms. List them with:

cat /sys/block/zram0/comp_algorithm

The most common options are lzo and zstd. lzo is fast; zstd compresses better. Choose zstd if you want capacity, lzo if you want speed.

Feel free to follow my blog at www.bboy.app

Have Fun