RT Cunningham

Blogging For As Long As I'm Able

The Zram Swapping Mechanism on Linux

Tagged with computers, linux, raspberry pi, zram on December 8, 2023

Zram Using zram is a much better idea than using other memory swapping mechanisms because nothing is written to physical media. Very few Linux distributions use zram by default, but all computer systems feature some kind of memory swapping mechanism.

On Linux, it’s either a swap partition, a swap file, or a memory block device, depending on the distribution. Swapping shouldn’t come into play until the computer runs out of RAM. Unfortunately, a small amount of swapping almost always occurs, even when it isn’t needed.

As long as your Linux computer has four or more gigabytes of RAM, using zram is even better than using zswap, which compresses pages in memory before storing them on physical media. Most affordable laptop computers today come with a minimum of four gigabytes of RAM.

Disable Swap

The easiest way to install zram is to start off by disabling any kind of swap mechanism in place. In regular Intel and AMD systems, you just need to comment out the line in /etc/fstab. With Raspberry Pi OS, you have to disable the service:

sudo systemctl disable dphys-swapfile
sudo apt purge dphys-swapfile

You can delete swap files and partitions if you want to reclaim space, but it isn’t necessary.

Install Zram

While distribution repositories have one or more applications to install zram, a script at GitHub works much better for this purpose. Zram can be used for more things than swapping memory.

This script will automatically allocate half of the RAM for its use, and then compress that half. If you use the “free” command, it would appear you have more swap space than non-swap space. Make sure git is installed before doing this:

git clone https://github.com/foundObjects/zram-swap.git
cd zram-swap && sudo ./install.sh

I’m using zram on both my laptop computer and my Raspberry Pi 400 keyboard computer. Both computers use nothing but solid-state drives. I like the fact that nothing gets written to the physical drive, and what’s stored in memory disappears when rebooting or shutting down.

While the instructions above work for the Raspberry Pi computers as well, there’s another way to do it with them. First, install Pi-Apps. Next, run the Pi-Apps application. Finally, find the “More RAM” menu item under tools and run it. Pi-Apps is a good idea because it has scripts for dozens of applications designed to run on Raspberry Pi computers.

Image by PublicDomainPictures from Pixabay

← Previous ArticleNext Article →