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 
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/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 ...

[Unit]
Description=Configure zram swap device
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/zram_start
ExecStop=/usr/local/bin/zram_stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

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
100
$ cat /proc/sys/vm/swappiness
60
$ cat /proc/sys/vm/dirty_background_ratio
10
$ sudo cat /proc/sys/vm/dirty_ratio
20

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

vm.vfs_cache_pressure=500
vm.swappiness=100
vm.dirty_background_ratio=1
vm.dirty_ratio=50

5. Helpful

Thanks for reading! Read other posts?

» Next: Install Linux Mint 20.3 with custom partition layout

« Previous: Getting started with Arch Linux