How to use zram with Raspberry Pi

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 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

May be interested

  • Fix Raspberry Pi not connecting to WiFi / EthernetPhoto of Fix Raspberry Pi not connecting to WiFi / Ethernet
    if your raspberry pi doesn't connect to wifi or ethernet, you need to check your wifi router to see if the internet connection is working.
  • How to install Caliber Content Server on Raspberry PiPhoto of How to install Caliber Content Server on Raspberry Pi
    caliber is a great piece of software for managing your ebook collection. in today's article, tipsmake will guide you how to install caliber content server on the raspberry pi.
  • What is the Raspberry Pi and how is the Raspberry Pi used?Photo of What is the Raspberry Pi and how is the Raspberry Pi used?
    you can use the raspberry pi as a computer because everything needed has been built in. its broadcom bcm2835 soc processor includes cpu, gpu, ram, microsd card slot, wi-fi, bluetooth and 4 usb 2.0 ports.
  • How to rotate the Raspberry Pi screenPhoto of How to rotate the Raspberry Pi screen
    rotating the screen output of the raspberry pi is a task often asked when handling cases for a full-fledged raspberry pi touchscreen. this only involves a few relatively simple steps and can be completed in a matter of minutes.
  • How to properly turn off the Raspberry PiPhoto of How to properly turn off the Raspberry Pi
    as you may have noticed, there is no button to enable or disable the raspberry pi. your first instinct is probably to unplug the power cord, but this is highly discouraged.
  • How to install Java on a Raspberry PiPhoto of How to install Java on a Raspberry Pi
    there are two different java implementations, oracle java and openjdk. this tutorial explains how to install java (openjdk) on a raspberry pi with the latest raspbian operating system running on it.