Reminder: enable ZRAM on your Linux system to optimize RAM usage (and potentially save money)

4 min read Original article ↗

ZRAM zstd compression ram sticks

With the price of RAM getting out of control, it might be a good idea to remind Linux users to enable ZRAM so they can get better performance without upgrading memory, or save money on their next single board computer by selecting a board with the right amount of memory.

I had already written about the subject when I enabled ZRAM on a ODROID-XU4Q in 2018 using zram-config, and did the same on my Ubuntu laptop at the time. In recent days, I found Firefox crashing often due to running out of memory on my system with 16GB of RAM, and the Linux 7.0 release reminded me about ZRAM, since there were some related changes. So I decided to check the current swap configuration on my Ubuntu 24.04 laptop:

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 doesn’t look like a recent compression algorithm, and I think I’ve seen Zstandard compression used on other systems before. However, the zram-config utility appears to be an older solution, and it’s now been replaced with zram-tools. So I decided to replace it. If you haven’t already enabled ZRAM with zram-config, you don’t need to do that, but in my case, I had to disable swap and purge 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


Once done, I installed zram-tools:

sudo apt install zram-tools


and edited the /etc/default/zramswap file as follows:

# Compression algorithm selection

ALGO=zstd

# Specifies the amount of RAM that should be used for zram

# based on a percentage the total amount of available memory

# This takes precedence and overrides SIZE below

PERCENT=75

...

# Specifies the priority for the swap devices, see swapon(2)

# for more details. Higher number = higher priority

# This should probably be higher than hdd/ssd swaps.

PRIORITY=100


To be on the safe side, you may want to check zstd if supported by your kernel:

jaufranc@CNX-LAPTOP-5:~$ cat /sys/block/zram0/comp_algorithm

lzo-rle lzo lz4 lz4hc [zstd] deflate 842


I then restarted the service with the new parameters:

sudo systemctl start zramswap.service


Finally, let’s check if everything is enabled as expected:

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

jaufranc@CNX-LAPTOP-5:~$


It looks good. The swapfile on my NVMe SSD is not used anymore, but I’ll try to use my system that way, and only re-enable it in case the system runs out of memory.

Ubuntu zram laptop

Finally, I wanted to make sure it was enabled on my Raspberry Pi 5 with 2GB of RAM, and I forgot that it’s indeed 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@raspberrypi:~ $ free -mh

total used free shared buff/cache available

Mem: 2.0Gi 1.2Gi 213Mi 38Mi 794Mi 808Mi

Swap: 2.0Gi 178Mi 1.8Gi


Raspberry Pi ZRAM

Note the config for rpi-swap is in a different location: /etc/rpi/swap.conf, and follows a different format:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

#  This file is part of rpi-swap.

#

#  Defaults are provided as commented-out options. Local configuration

#  should be created by either modifying this file, or by creating "drop-ins" in

#  the swap.conf.d/ subdirectory. The latter is generally recommended.

#

#  See swap.conf(5) for details.

[Main]

#Mechanism=auto

[File]

#Path=/var/swap

#RamMultiplier=1

#MaxSizeMiB=2048

#MaxDiskPercent=50

#FixedSizeMiB=

[Zram]

#RamMultiplier=1

#MaxSizeMiB=2048

#FixedSizeMiB=

# Writeback settings (for zram+file mechanism):

#WritebackTrigger=auto

#WritebackInitialDelay=180min

#WritebackPeriodicInterval=24h


More details about this specific implementation can be found on GitHub. If you are using a different operating system on any SBC, you may want to check ZRAM (or  zswap) is enabled.

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress. We also use affiliate links in articles to earn commissions if you make a purchase after clicking on those links.