Zram swap on Arch Linux

Last edited on 2022-04-14 Tagged under  #arch   #linux 

Instead of creating a separate swap partition or using a swapfile, its possible to create a high-speed swap device in RAM itself with the Linux kernel module zram. Even though its been around for years, I'm learning about it for the first time and its pretty cool!

Let's go!

Zram creates a compressed block device in memory, and the RAM assigned to it is only used for swapping as-needed (until then, the RAM is still available for use by applications).

Swap data stored is compressed thereby allowing more data to be stored in RAM. Performing read/write operations in memory is both a speed boost and reduces wear on SSD drives, at the cost of more CPU-intensive activity.

My setup

  • OS is Arch Linux
  • Enabled on my Thinkpad X230 with 8GB RAM
  • Assign 8GB of memory to the zram0 device for swap
  • No other swap devices are in use

1. Start zram swap for first time

Disable zswap (similar name, different beast) to avoid it acting as a swap cache in front of zram ...

$ sudo bash -c "echo 0 > /sys/module/zswap/parameters/enabled"

Disable any active swaps ...

$ sudo swapoff --all

Check if any swap devices exist in fstab ...

$ grep swap /etc/fstab

If so, comment out the swap entries to disable.

Load module ...

$ sudo modprobe zram num_devices=1

Show supported compression algorithms ...

$ cat /sys/block/zram0/comp_algorithm

Set compression algorithm (example: zstd) ...

$ sudo bash -c "echo zstd > /sys/block/zram0/comp_algorithm"

Set disk size (example: 8G) ...

$ sudo bash -c "echo 8G > /sys/block/zram0/disksize"

Activate zram0 device with the highest priority setting of 32767 ...

$ sudo mkswap --label zram0 /dev/zram0
$ sudo swapon --priority 32767 /dev/zram0

Examine device using zramctl command (from package util-linux) ...

$ zramctl 
/dev/zram0 zstd            8G   4K   64B    4K       4 [SWAP]

2. Stop zram swap

Deactivate zram0 ...

$ sudo swapoff /dev/zram0

Free all memory formerly allocated to device and reset disksize to zero ...

$ sudo bash -c "echo 1 > /sys/block/zram0/reset"

Unload module ...

$ sudo modprobe -r zram

3. Start at boot

Auto-start creation and activation of zram0 at boot time.

Write the steps above in scripts zram_start and zram_stop, and place these scripts in /usr/local/bin.

Create file /etc/systemd/system/zram-swap.service with ...

Description=Configure zram swap device



Enable service ...

$ sudo systemctl enable zram-swap.service

4. Kernel parameters

Tune the kernel to take advantage of the new superfast swap.

Settings of interest:

  • vm.vfs_cache_pressure - increase frequency of clearing caches to free RAM
  • vm.swappiness - increase percentage to start using zram earlier
  • vm.dirty_background_ratio and vm.dirty_ratio - amount of memory pages permitted to be "dirty" before writing to zram

Before I changed any settings, these were my system defaults ...

$ cat /proc/sys/vm/vfs_cache_pressure
$ cat /proc/sys/vm/swappiness
$ cat /proc/sys/vm/dirty_background_ratio
$ sudo cat /proc/sys/vm/dirty_ratio

These kernel parameters would formerly be set in /etc/sysctl.conf. From version 207, systemd has dropped sysctl.conf in favour of /etc/sysctl.d/*.conf files.

Copy an existing sysctl.conf to /etc/sysctl.d/99-sysctl.conf. Or create this file from scratch.

These are my current settings in 99-sysctl.conf ...


5. Helpful

Thanks for reading! Read other posts?

» Next: Install Linux Mint 20.3 with custom partition layout

« Previous: Getting started with Arch Linux