zram

From ArchWiki
(Redirected from ZRAM)

Zram, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk with on-the-fly disk compression. The block device created with zram can then be used for swap or as a general-purpose RAM disk. The two most common uses for zram are for the storage of temporary files (/tmp) and as a swap device. Initially, zram had only the latter function, hence the original name "compcache" ("compressed cache").

Usage as swap

Disable zswap

Since it is enabled by default, disable zswap if you decide to use zram to avoid zswap acting as a swap cache in front of it. Having both enabled also results in incorrect zramctl(8) statistics as zram remains mostly unused; this is because zswap intercepts and compresses memory pages being swapped out before they can reach zram.

Automatically

zram-generator provides a systemd-zram-setup@.service unit to automatically initialize zram devices without users needing to enable/start the template or its instances. See zram-generator(8) and zram-generator.conf(5).

For example, to create a zram swap device using zstd and the entire available ram, install zram-generator, then create /etc/systemd/zram-generator.conf with the following:

/etc/systemd/zram-generator.conf
[zram0]
zram-size = ram
compression-algorithm = zstd

Reboot, then check the swap status of your configured /dev/zramN devices by reading the unit status of the systemd-zram-setup@zramN.service instance(s), or by using zramctl(8).

Alternatively, zramswapAUR provides an automated script for setting up a swap with a higher priority and a default size of 20% of the RAM size of your system. To do this automatically on every boot, enable zramswap.service. zramdAUR allows to setup zram automatically using zstd compression by default, its configuration can be changed at /etc/default/zramd. It can be started at boot by enabling the zramd.service unit.

Manually

To set up one lz4 compressed zram device with 32GiB capacity and a higher-than-normal priority (only for the current session):

# modprobe zram
# echo lz4 > /sys/block/zram0/comp_algorithm
# echo 32G > /sys/block/zram0/disksize
# mkswap --label zram0 /dev/zram0
# swapon --priority 100 /dev/zram0

To disable it again, either reboot or run

# swapoff /dev/zram0
# rmmod zram

A detailed explanation of all steps, options and potential problems is provided in the official documentation of the zram module.

Swap on zram using a udev rule

The example below describes how to set up swap on zram automatically at boot with a single udev rule. No extra package should be needed to make this work.

Explicitly load the module at boot:

/etc/modules-load.d/zram.conf
zram

Configure the number of /dev/zram nodes you need:

/etc/modprobe.d/zram.conf
options zram num_devices=2

Create the udev rule as shown in the example:

/etc/udev/rules.d/99-zram.rules
KERNEL=="zram0", ATTR{comp_algorithm}="zstd", ATTR{disksize}="512M", RUN="/usr/bin/mkswap /dev/zram0", TAG+="systemd"
KERNEL=="zram1", ATTR{comp_algorithm}="zstd", ATTR{disksize}="512M", RUN="/usr/bin/mkswap /dev/zram1", TAG+="systemd"

Add /dev/zram to your fstab.

/etc/fstab
/dev/zram0 none swap defaults 0 0
/dev/zram1 none swap defaults 0 0