Using Zram on your Raspberry Pi To Improve Performance

New Article

2018/10/30

RaspberryPi Performance

Learn how to activate ZRAM on your Raspberry Pi by using a simple and easy bash script to increase the memory performance.

About ZRAM

If a linux system runs out of free RAM it uses a so called swap to write to. This swap file is normally located on a rather slow disk drive. Its purpose is to prevent your whole system to freeze or crash in case no memory can be allocated in the RAM. As you can imagine it is much slower than accessing the computers RAM.

ZRAM is a technology which uses a combination of RAM and swap with compression in between. To overcome the slow disk part a disk is placed in the RAM storage. This disk is then used a swap memory. It may seem counterintuitive as you could just use the RAM from the beginning but in the next step this disk is also getting compressed.

Ultimately the amount of data which has be read from or written to RAM (respective RAM disk) is decreased. There is a overhead in CPU utilization though but this way it is still faster and memory efficient than using no compression at all. You can learn more about ZRAM in the wikipedia article.

Get the script for automatic installation

Running the installation script

There are already many scripts which try to make the activation of ZRAM on your system as convienent as possible. The only thing you need to do is downloading the following script and run it as sudo:

Done! ZRAM will be added automatically on every reboot from now on.

I have forked the script from the original author in order to keep it online just in case.

Running the uninstallation script

If you want to get rid of ZRAM you can do the following:

What does this script do?

The install.sh downloads a zram.sh script and sets its flag to be executable. Afterwards a systemd service is created in the /etc/systemd/system path named zram.service. Through this service it is possible to run the script on every bootup.

Within the install.sh we reload the systemd daemon just in case so it recongnized the service file and enable it to be started on system boot.

# rpi_zram install

#Download the script and copy to /usr/bin/ folder
sudo wget -O /usr/bin/zram.sh
https://raw.githubusercontent.com/daviel/rpi_zram/master/zram.sh

#make file executable
sudo chmod +x /usr/bin/zram.sh

#add systemd service

sudo tee /etc/systemd/system/zram.service <<-'EOF'
[Unit]
Description=zram Service
;After=network-online.target
;Wants=network-online.target systemd-networkd-wait-online.service
[Service]
Type=simple
ExecStart=/usr/bin/zram.sh
[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable zram.service

When we take a look at the zram.sh we can see that it does query the count of CPU cores on the system and enables the zram kernel module. Every existing swap will be turned off and a zram swap will be enabled for every CPU core. Every swap file will be the RAM size divided by CPU core count.

#!/bin/bash
# zram.sh
cores=$(nproc --all)
modprobe zram num_devices=$cores

swapoff -a

totalmem=`free | grep -e "^Mem:" | awk '{print $2}'`
mem=$(( ($totalmem / $cores)* 1024 ))

core=0
while [ $core -lt $cores ]; do
echo $mem > /sys/block/zram$core/disksize
mkswap /dev/zram$core
swapon -p 5 /dev/zram$core
let core=core+1
done

And that's it!