To learn what zram is check Arch Wiki and official documentation.

Preparation

Disable zswap by adding zswap.enabled=0 to your kernel parameters.

Zram configuration

First, explicitly load the module:

echo 'zram' > /etc/modules-load.d/zram.conf

Create udev rule:

echo 'ACTION=="add", KERNEL=="zram0", ATTR{comp_algorithm}="zstd", ATTR{disksize}="4G", RUN="/usr/bin/mkswap -U clear /dev/%k", TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules

Adjust disksize attribute if necessary.

Add /dev/zram0 to fstab:

cat << EOF >> /etc/fstab

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

That’s it, reboot and you should see your zram as swap:

$ free -h
               total        used        free      shared  buff/cache   available
Mem:            31Gi       3.8Gi        25Gi       123Mi       2.6Gi        27Gi
Swap:          4.0Gi          0B       4.0Gi

Optimizing swap on zram

To take full potential of zram we can adjust system’s swap parameters:

vim /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

Check Arch Wiki for explanation.

Checking statistics

You can check zram statistics using zramctl:

$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd            4G   4K   64B   20K      12 [SWAP]
  • DISKSIZE - How much uncompressed data can be stored in zram
  • DATA - currently stored (uncompressed) data
  • COMPR - space used after compression
  • TOTAL - total space used, includint metadata

Sources

Arch Wiki - zram