How to use zram with Raspberry Pi

zram is a Linux kernel module designed to create a compressed block inside RAM, used either as swap memory or as a temporary drive.

Data inserted into this part of memory is automatically compressed. This compression allows you to put more data in your memory.

This tutorial will show you how to use zram on Raspberry Pi to increase the amount of data that can be stored in memory.

How to use zram with Raspberry Pi Picture 1How to use zram with Raspberry Pi Picture 1

Prepare Raspberry Pi for zram

The process of setting up ZRAM on the Raspberry Pi is relatively straightforward, thanks to a few things.

  1. First, zram is part of the Linux kernel, the foundation of the Raspberry Pi OS.
  2. Second, the script will handle all the guesswork, roughly calculating how much disk space ZRAM should be.

Step 1. The first task is to make sure that the Raspberry Pi is running an up-to-date operating system version. A full update is possible by running the following two commands.

sudo apt update sudo apt full-upgrade

Step 2. Now you should install any extra packages to setup zram on your device.

Just use git to copy the script to use to the Raspberry Pi.

sudo apt install git

Step 3. Since zram is a kernel module, it is necessary to restart the Raspberry Pi after running the full upgrade. Restart the Raspberry Pi with the command below.

sudo reboot

Step 4. Once the Raspberry Pi has finished rebooting, you can download the script that will be used to set up the zram drive.

The script the article is using is written by foundObjects from GitHub. This script calculates the zram size using the amount of free memory, multiplied by the compression factor and the percentage of RAM reserved for zram.

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

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

Setting up zram on Raspberry Pi

You have now updated your operating system and downloaded the zram-swap script to your Raspberry Pi. Next, let's set it up. Thanks to the script in use, the setup can be completed in a few simple steps.

Step 1. Let's change the directory containing the script just copied.

cd zram-swap

Step 2. Now, once in the correct directory, you can run the zram-swap script to install itself.

This installation script will set up the service to create the zram drive at startup. The script will also set it up so that the operating system uses that zram drive as part of swap memory.

sudo ./install.sh

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

The LZ4 compression algorithm is the best compromise between performance and compression speed, especially for a low-powered device like the Raspberry Pi.

Verify Raspberry Pi's zram

Now, after running the installation script, you should verify that the Raspberry Pi now has access to the zram swap.

Fortunately, the operating system has a number of built-in features that allow you to check the status of swap memory quickly.

Step 1. Retrieving the Raspberry Pi's zram swap status is a simple process.

All you need to do is run the following command to return the contents of the file '/proc/swaps'.

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

sudo cat /proc/swaps

Step 2. Using the previous command, you will see something similar to the one shown below.

You should have two swap files. One is the default one on your own file system.

The second one is the created zram drive that exists in RAM on the Raspberry Pi.

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

You can see the zram drive is named '/dev/zram0' , the size is shown in kilobytes and will vary depending on the Raspberry Pi.

You should also note that its priority is higher than drive-based swap. This ensures the OS uses the zram drive before using the slower drive-based swap.

4.4 ★ | 15 Vote