zram - ArchWiki

Jump to content
Toggle the table of contents

# zram

3 languages

- 日本語
- Русский
- 中文（简体）

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

This article or section needs expansion.

Reason: Add a easily-understandable suggested size for the zram device. (Discuss in Talk:Installation guide#Mention zram)

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:

- 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.
- 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. Therefore, it's recommended to permanently disable zswap using the kernel parameter or sysfs setting before starting.
- 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

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.

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

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

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 32 GiB of uncompressed data.
- DATA = 1.9G: currently, 1.9 GiB (uncompressed) of data is being stored in this zram device
- COMPR = 318.6M: the 1.9 GiB uncompressed data was compressed to 318.6 MiB
- TOTAL = 424.9M: including metadata, the 1.9 GiB of uncompressed data is using up 424.9 MiB 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 pull request 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.

### Enabling a backing device for a zram block

zram can be configured to push incompressible pages to a specified block device when under memory pressure.

To add a backing device manually:

    # echo /dev/sdX > /sys/block/zram0/backing_dev

To add a backing device to your zram block device using zram-generator, update `/etc/systemd/zram-generator.conf` with the following under your `[zramX]` device you want the backing device added to:

    /etc/systemd/zram-generator.conf

    writeback-device=/dev/disk/by-partuuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

### Using zram for non-swap purposes

zram can also be used as a generic RAM-backed block device, e.g. a `/dev/ram` with less physical memory usage, but slightly lower performance. However there are some caveats:

- There is no partition table support (no automatic creation of `/dev/zramxpy`).
- The block size is fixed to 4 kiB.

The obvious way around this is to stack a loop device on-top the zram, using losetup, specifying the desired block size using the `-b` option and the `-P` option to process partition tables and automatic creation of the partition loop devices.

    # zramctl -f -s NG

    /dev/zramx

Copy the disk image to the new `/dev/zram`:

The factual accuracy of this article or section is disputed.

Reason: Why is the following block size set to 512? (Discuss in Talk:Zram)

    # losetup -f -b 512 -P /dev/zramx

    # ls /dev/loop*

    /dev/loop0 /dev/loop0p1 /dev/loop0p2

    # mount /dev/loop0p1 /mnt/boot
    # mount /dev/loop0p2 /mnt/root

Note:

- The zram device numbering depends on pre-existing zram devices and its size should be enough to hold the disk image.
- The output from `ls /dev/loop*` depends on the contents of the disk image.

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

Hidden categories:

- Pages or sections flagged with Template:Expansion
- Pages or sections flagged with Template:Accuracy