Using zram with the Raspberry Pi

This guide will show you how to utilize zram on your Raspberry Pi to increase the amount of data you can fit within memory.

Raspberry Pi zram

zram is a Linux kernel module that is designed to create a compressed block within your RAM that can be used as swap memory or as a temporary disk.

Data inserted into this portion of the memory will automatically be compressed. This compression allows you to fit more data into your memory.

Swap memory makes up a part of Linux’s virtual memory system and is used as a spill over to prevent out of memory errors.

Typically the swap memory will exist purely on the storage device such as the Raspberry Pi’s SD card and cause significant slowdowns.

In this case, the zram disk will act as an additional part of the swap memory. This partition will still retain the IO benefits of traditional RAM but with the bonus of compression.

This compression is not free and comes at the cost of additional CPU usage. Every time memory is accessed within your zram the CPU will need to be used to compress or decompress the data.

Equipment

Below is the list of recommended equipment that we used when setting up our Raspberry Pi with a zram swap file.

Recommended

Optional

This tutorial was tested on a Raspberry Pi 400, running an updated version of Raspberry Pi OS.

Preparing the Raspberry Pi for zram

The process of setting up ZRAM on your Raspberry Pi is relatively simple, thanks to a couple of things.

First is that zram is a part of the Linux kernel which is used as the base of Raspberry Pi OS.

Secondly, a script will handle all of the guesswork for us, calculating roughly what should be a good size for the ZRAM disk.

1. Our first task to ensure that our Raspberry Pi is running an up-to-date version of the operating system.

We can do a full update by running the following two commands.

sudo apt update
sudo apt full-upgrade

2. We should now install any additional packages to set up zram on our device.

We should only need to use git to clone the script that we will be using to our Raspberry Pi.

sudo apt install git

3. As zram is a kernel module, we should restart our Raspberry Pi after running a full upgrade.

This is because we could have potentially updated the kernel, and we want to reduce the chance of running into issues.

Restart the Raspberry Pi by using the command below.

sudo reboot

4. Once your Raspberry Pi has finished rebooting, we can download the script that we will use to set up the zram disk.

The script we are using is written by foundObjects from GitHub.

This script calculates your zram size by using the amount of free memory, multiplied by the compression factor and the percentage of RAM dedicated to zram.

Clone the zram-swap script from its GitHub repository by running the following command on your Raspberry Pi.

git clone https://github.com/foundObjects/zram-swap

Setting up zram on Your Raspberry Pi

Now that we have updated our operating system and download the zram swap script to our Raspberry Pi we can set it up.

Thanks to the script that we are using, the setup process can be completed in a couple of simple steps.

1. Let us change to the directory containing the script we just cloned.

cd zram-swap

2. Now that we are in the correct directory, we can run the zram-swap script to install itself.

This install script will set up the service that will create the zram disk at startup.

The script will also set it up so that the operating system will use that zram disk as a part of the swap memory.

sudo ./install.sh

By default, this script will replace half of your memory with the zram disk and compressing the data using the LZ4 compression algorithm.

The lz4 compression algorithm is the best compromise between performance and compression rate, especially for a low powered device such as the Raspberry Pi.

Verifying your Raspberry Pi’s zram

Now that we have run the install script, we should verify that our Raspberry Pi now has access to our zram swap.

Luckily the operating system has several built-in features that allow us to check our swap memory status quickly .

1. Retrieving the status of your Raspberry Pi’s zram swap is a simple process.

All you need to do is run the following command to return the “/proc/swaps” file’s contents.

This file contains the information about your swap files, including their type, size, and priority.

sudo cat /proc/swaps

2. Using the previous command, you should see something as we have below.

You should have two swap files. One is the default one that is present on your filesystem itself.

The second one is the zram disk that we created that exists within the RAM on your Raspberry Pi.

Filename                                Type            Size    Used    Priority
/var/swap                               file            102396  0       -2
/dev/zram0                              partition       4899744 0       5

You can see the zram disk named as “/dev/zram0“, the size is displayed in kilobytes and will be different depending on your Raspberry Pi.

You should also note that its priority is higher than the disk-based swap. This ensures the operating system uses our zram disk before resorting to the slower disk-based swap.

Conclusion

At this stage, you should have got your Raspberry Pi set up with a swap partition using zram.

This zram being compressed allows you to fit more data within your Pi’s RAM. This increase comes at the cost of CPU usage whenever it is accessed.

Using zram is incredibly useful for squeezing out more memory from your device. It allows you to extend your older Raspberry Pi’s lifespan by enabling it to squeeze more out of its memory.

If you have run into any issues with getting zram set up for your Raspberry Pi, feel free to leave a comment below.

5 Comments

  1. Avatar for Tom B Maker
    Tom B Maker on

    I assume zram would not work on Pi Zero W version 2 with its 512K memory.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Tom,

      There is no real reason this wouldn’t work on the Raspberry Pi Zero W. One of the drawbacks however is the increased CPU usage which might hurt performance with the weak processor that is in that version.

      Cheers,
      Emmet

  2. Avatar for Brian C(WarHawk8080)
    Brian C(WarHawk8080) on

    Very cool…it makes the system much more stable as if/when it needs to swap it can use the compressed space. The processor usage to compress/uncompress is still MUCH faster than swapping to a SD card. I put this and log2ram on all my SBC’s to reduce writes to my SD cards.

    Great writeup!!!!

  3. Avatar for markfree
    markfree on

    I wonder if reducing RAM by half and raising CPU usage to make it Swap is a good trade off.

    1. Avatar for JustANobody
      JustANobody on

      Obviously “it depends” in my case, my CPU usage has generally been fairly low but memory usage keeps popping into the swap space. I’m running pi-hole and unifi controller on a Pi3 with 1G of ram. Trying to minimize SD writes as I had one fail recently and I suspect all the logging and swapping had something to do with it. Like earlier commentator, I’m also using the log2ram utility. Will see if it passes the test of time.
      To OP: Thank you for the writeup, concise and easy to follow. Got it running in about as much time as it took to read.

Leave a Reply

Your email address will not be published. Required fields are marked *