zram - ArchWiki

Jump to content
From ArchWiki

Related articles

- Swap
- zswap

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.

#### Using systemd-swap

This section is being considered for removal.

Reason: This is not mainly used to handle zram, but should probably not be kept on any page either since its author discourages its use. (Discuss in Talk:Zram)
Note: The author now recommends using zram-generator instead, due to low commit frequency and zram covering the needs of most users.

systemd-swap is a script for creating hybrid swap space from zram swaps, swap files and swap partitions. It is not affiliated with the systemd project.

Install the systemd-swap package. Uncomment and set `swapfc_enabled=1` in the Swap File Chunked section of `/etc/systemd/swap.conf`. Start/enable the `systemd-swap` service.

Visit the author's GitHub page for more information and setting up the recommended configuration.

Note:

- If the journal keeps showing the following warning `systemd-swap[..]: WARN: swapFC: ENOSPC` and no swap file is being created, you need to uncomment and set `swapfc_force_preallocated=1` in `/etc/systemd/swap.conf`.
- The swap file created by systemd-swap cannot be easily used for hibernation. See systemd-swap issue 85.

### 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
    # modprobe -r 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

    ACTION=="add", KERNEL=="zram[0-1]", ATTR{comp_algorithm}="zstd", ATTR{disksize}="512M", RUN="/usr/bin/mkswap -U clear /dev/%k", TAG+="systemd"

Add `/dev/zram` to your fstab.

    /etc/fstab

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

Category:

- Kernel

Hidden category:

- Sections flagged with Template:Remove