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.
Prepare Raspberry Pi for zram
The process of setting up ZRAM on the Raspberry Pi is relatively straightforward, thanks to a few things.
- First, zram is part of the Linux kernel, the foundation of the Raspberry Pi OS.
- Second, the script will handle all the guesswork, roughly calculating how much disk space ZRAM should be.
1. The first task is to make sure that the Raspberry Pi is running an up-to-date version of the operating system. A full update is possible by running the following two commands.
sudo apt update sudo apt full-upgrade
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
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.
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.
1. Change to the directory containing the copied script.
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.
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.
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
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 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.