With RAM prices rising, Linux users are advised to enable ZRAM for improved performance without needing a memory upgrade. This tip can also help save money when choosing a single board computer with the right memory size. Previously, I wrote about this after enabling ZRAM on an ODROID-XU4Q in 2018 using zram-config and did the same on my Ubuntu laptop. Recently, running out of memory on my system with 16GB RAM caused frequent Firefox crashes, and the Linux 7.0 release reminded me of ZRAM. I checked my Ubuntu 24.04 laptop’s swap configuration:
“`
jaufranc@CNX-LAPTOP-5:~$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 7.6G 6.6G 2.1G 2.1G [SWAP]
jaufranc@CNX-LAPTOP-5:~$ swapon
NAME TYPE SIZE USED PRIO
/swapfile file 8G 5.6G -2
/dev/zram0 partition 7.6G 7.3G 5
jaufranc@CNX-LAPTOP-5:~$ free -mh
total used free shared buff/cache available
Mem: 15Gi 9.6Gi 4.3Gi 2.4Gi 3.4Gi 5.7Gi
Swap: 15Gi 12Gi 2.7Gi
“`
LZO is not a recent compression algorithm, and I’ve seen Zstandard used before. The zram-config tool is outdated and has been replaced by zram-tools. I swapped tools by disabling swap and purging the package:
“`
sudo swapoff -a
sudo swapoff /dev/zram0 2>/dev/null || true
echo 1 | sudo tee /sys/block/zram0/reset 2>/dev/null || true
sudo modprobe -r zram
sudo apt purge –autoremove zram-config
“`
After that, I installed zram-tools:
“`
sudo apt install zram-tools
“`
I edited the `/etc/default/zramswap` file:
“`
# Compression algorithm selection
ALGO=zstd
# Specifies RAM percentage for zram
# Takes precedence and overrides SIZE below
PERCENT=75
…
# Swap devices priority; higher number = higher priority
# Greater than hdd/ssd swaps.
PRIORITY=100
“`
Ensure zstd is supported by the kernel:
“`
jaufranc@CNX-LAPTOP-5:~$ cat /sys/block/zram0/comp_algorithm
lzo-rle lzo lz4 lz4hc [zstd] deflate 842
“`
I restarted the service:
“`
sudo systemctl start zramswap.service
“`
Verifying the new setup:
“`
jaufranc@CNX-LAPTOP-5:~$ cat /sys/block/zram0/comp_algorithm
lzo-rle lzo lz4 lz4hc [zstd] deflate 842
jaufranc@CNX-LAPTOP-5:~$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd 11.4G 7G 1.4G 1.5G [SWAP]
jaufranc@CNX-LAPTOP-5:~$ swapon –show
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 11.4G 7.8G 100
jaufranc@CNX-LAPTOP-5:~$ free -mh
total used free shared buff/cache available
Mem: 15Gi 12Gi 561Mi 2.4Gi 3.8Gi 2.8Gi
Swap: 11Gi 7.8Gi 3.6Gi
“`
This setup removed the use of NVMe SSD swapfile. On a Raspberry Pi 5 with 2GB RAM, ZRAM is enabled by default on Raspberry Pi OS:
“`
pi@raspberrypi:~ $ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd 2G 181.9M 22.7M 29.6M 4 [SWAP]
pi@raspberry
