Using zram is a much better idea than using swap files or swap partitions because nothing is written to physical media. As long as your Linux computer has four or more gigabytes of RAM, it’s even better than using zswap, which compresses pages in memory before storing them in files or partitions.
Some Linux distributions use partitions and some use files. Raspberry Pi OS uses a service, which probably ends up being a file (but I never checked).
The easiest way to install zram is to start off by disabling any kind of swap system in place. In regular Intel/AMD 32 and 64-bit 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 files and partitions if you want to reclaim space, but it isn’t necessary.
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
If half of your RAM is too much or too little, you can adjust it by editing this file:
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 disk, and what’s stored in memory disappears when rebooting.
While the instructions above work for the Raspberry Pi computers, 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. Using Pi-Apps won’t work, of course, if you’re running headless.
Image by email@example.com Larry Ewing and The GIMP, CC0, via Wikimedia Commons