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 former function, hence the original name "compcache" ("compressed cache").

## Usage as swap

Initially the created zram block device does not reserve or use any RAM. Only as files need or want to be swapped out, they will be compressed and moved into the zram block device. The zram block device will then dynamically grow or shrink as required.

For example, consider an example system with 32 GiB RAM where zram is configured with 64 GiB capacity. Assuming that zstd employs a 1:4 compression ratio, the actual compressed zram block size on physical RAM when fully occupied will be around 16 GiB. Therefore:

- RAM and zram completely occupied: 16 GiB RAM + 64 GiB zram (~16 GiB in RAM)
- Normal usage without swapping: 32 GiB RAM + 0 GiB zram
- Normal usage with light swapping: 30 GiB RAM + 8 GiB zram (~2 GiB in RAM)
- Without any zram configuration: 32 GiB RAM

Therefore, zram always offers the advantage of being able to store more content in RAM.

Note:

- If the related zswap kernel feature remains enabled, it will prevent zram from being used effectively. This is because zswap functions as a swap cache in front of zram, intercepting and compressing evicted memory pages before they can reach zram. Despite the output of zramctl(8), most of zswap is unused in this circumstance.
- As discussed above, when configuring zram, the size of the zram device controls the maximum uncompressed amount of data it can store, NOT the maximum compressed size. You can configure the zram's size to be equal to or even greater than your system's physical RAM capacity, as long as the compressed size on physical RAM will not exceed your system's physical RAM capacity.
- Hibernating to swap on zram is not supported, even when zram is configured with a backing device on permanent storage. logind will protect against trying to hibernate to a swap space on zram.

### Manually

First, disable zswap via sysfs:

    # echo 0 > /sys/module/zswap/parameters/enabled

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

    # modprobe zram
    # zramctl /dev/zram0 --algorithm zstd --size 32G
    # mkswap -U clear /dev/zram0
    # swapon --priority 100 /dev/zram0

To disable it again, either reboot or run:

    # swapoff /dev/zram0
    # modprobe -r zram
    # echo 1 > /sys/module/zswap/parameters/enabled

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

For a permanent solution, use a method from one of the following sections.

### 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.

Before starting, permanently disable zswap using the kernel parameter or sysctl knob.

Explicitly load the module at boot:

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

    zram

Create the following udev rule adjusting the `disksize` attribute as necessary:

    /etc/udev/rules.d/99-zram.rules

    ACTION=="add", KERNEL=="zram0", ATTR{comp_algorithm}="zstd", ATTR{disksize}="4G", RUN="/usr/bin/mkswap -U clear /dev/%k", TAG+="systemd"

Add `/dev/zram` to your fstab with a higher than default priority:

    /etc/fstab

    /dev/zram0 none swap defaults,pri=100 0 0

Note: The swap space on zram cannot be referred to by its LABEL or UUID in fstab since udev does not create `/dev/disk/by-label/*` and `/dev/disk/by-uuid/*` symlinks for zram devices.

### Using zram-generator

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).

Before starting, permanently disable zswap using the kernel parameter or sysctl knob. [1]

Then, to create a zram swap device using `zstd` and half of 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 / 2
    compression-algorithm = zstd
    swap-priority = 100
    fs-type = swap

Run daemon-reload, then start your configured `systemd-zram-setup@zramN.service` instance(s).

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

### Using zramswap

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`.

### Using zramd

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.

## Tips and tricks

### Checking zram statistics

Use zramctl(8). Example:

    $ zramctl

    NAME       ALGORITHM DISKSIZE  DATA  COMPR  TOTAL STREAMS MOUNTPOINT
    /dev/zram0 zstd           32G  1.9G 318.6M 424.9M      16 [SWAP]

- DISKSIZE = 32G: this zram device will store up to 32G of uncompressed data.
- DATA = 1.9G: currently, 1.9G (uncompressed) of data is being stored in this zram device
- COMPR = 318.6M: the 1.9G uncompressed data was compressed to 318.6M
- TOTAL = 424.9M: including metadata, the 1.9G of uncompressed data is using up 424.9M of physical RAM

### Multiple zram devices

By default, loading the `zram` module creates a single `/dev/zram0` device.

If you need more than one `/dev/zram` device, specify the amount using the `num_devices` kernel module parameter or add them as needed afterwards.

### Optimizing swap on zram

Since zram behaves differently than disk swap, we can configure the system's swap to take full potential of the zram advantages:

    /etc/sysctl.d/99-vm-zram-parameters.conf

    vm.swappiness = 180
    vm.watermark_boost_factor = 0
    vm.watermark_scale_factor = 125
    vm.page-cluster = 0

Explanation of the configuration:

These values are what Pop!_OS uses. That Pop!_OS GitHub PR also links to some testing done by users on r/Fedora, which determined that `vm.page-cluster = 0` is ideal. They also found a high swappiness value to be ideal, which matches what is suggested by the kernel docs:

"The default value is 60. For in-memory swap, like zram or zswap, as well as hybrid setups that have swap on faster devices than the filesystem, values beyond 100 can be considered. For example, if the random IO against the swap device is on average 2x faster than IO from the filesystem, swappiness should be 133 (x + 2x = 200, 2x = 133.33)."

On a system with a hard drive, random IO against the zswap device would be orders of magnitude faster than IO against the filesystem, so swappiness should be ~200. Even on a system with a fast SSD, a high swappiness value may be ideal.

## See also

- Wikipedia:zram
- https://github.com/pop-os/default-settings/pull/163
- https://www.reddit.com/r/pop_os/comments/znh9n6/help_test_a_zram_optimization_for_pop_os/

Category:

- Kernel