Most Linux systems tend to use a little bit of storage on the hard drive as swap space, where temporary files can be stored by the system and then easily recalled later. This speeds up operations, without needing to take up any space inside of the physical memory (RAM). On a Raspberry Pi, this same convention can prove problematic due to the majority of devices relying on an SD card as the primary (or only) storage device.
SD cards are not as fast as hard drives or solid state drives, so storing swap space on them is not ideal for speedy operations. To tackle this problem, users can enable the zram module inside of the Linux kernel. Zram can store swap space inside of the physical memory, making these temporary files and other data able to be recalled very quickly by system processes. Zram also compresses the storage inside of swap space, so that it does not tie up much of our precious RAM. This can technically be enabled on any system, but proves especially useful on Raspberry Pi’s that use SD cards. In this tutorial, you will see how to enable ZRAM on a Raspberry Pi.
In this tutorial you will learn:
- How to enable ZRAM on a Raspberry Pi
|Category||Requirements, Conventions or Software Version Used|
|Software||zram kernel module|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Enable zram on a Raspberry Pi step by step instructions
The easiest way to configure zram on a Raspberry Pi would be with the help of the zram-swap script available on GitHub. It works similarly to the
zram-configpackage available on Ubuntu systems. Since we are working with Raspberry Pi OS, this is the most user friendly way of configuring zram.
- Start by installing
gitin case your Raspberry Pi does not already have it, by executing:
$ sudo apt update $ sudo apt install git
- Clone the git repository mentioned earlier:
$ git clone https://github.com/foundObjects/zram-swap
- Go into the newly created
zram-swapdirectory and execute the installation script:
$ cd zram-swap/ $ sudo ./install.sh
This will utilize LZ4 compression by default, which is the best for performance on the Raspberry Pi. It will also replace half of the Raspberry Pi’s physical memory with zram. You can easily edit the
zram-swap.shfile to tinker with the settings as you see fit. The script contains many comments that document all of the configured settings.
- After successful execution of the script, check to make sure your system is now utilizing zram by executing:
$ sudo cat /proc/swaps
While most of the configuration has been handled for us by a script, for more advanced usage and to configure granular zram settings, you can use the
zramctlcommand in your Raspberry Pi’s terminal. See a list of options when you execute
In this tutorial, we saw how to enable zram on a Raspberry Pi system. For everyday I/O operating, utilizing zram often yields a significant performance increase over storing swap space on the Raspberry Pi’s SD card. For those with a hard drive plugged into the Raspberry Pi, this may not matter if the swap space is stored there, but those with only an SD card will be delighted by the speed increase of using zram.