This is an article in a series exploring the Raspberry Pi 5 used as a desktop Mini PC. We’re using the 4GB model of the Raspberry Pi 5 running the Raspberry Pi OS distribution. Our Pi is housed in a Pironman 5 Mini PC case from SunFounder. One of the advantages of the case is that it enables me to boot the Pi from a fast NVMe drive rather than an SD card.
In my previous article, I explored increasing the size of the swap file when using the Raspberry Pi OS. When using the Raspberry Pi 5 as a desktop machine with the 4GB model, the system can freeze easily and suddenly if I have too many applications running and/or too many Chromium tabs. When the RAM fills up, the entire system becomes unresponsive. One solution is to increase the swap file to 2GB which works well as the swap file also sits on my fast NVMe drive. But if you’re running the Pi 5 from an SD card, I suggest you consider an alternative approach.
This script enables ZRAM and attempts to optimize its effectiveness on Raspberry Pi computers. It creates a ZRAM swapspace that is four times the size of your RAM. Some of that space may not be available though as it depends on the compression ratio obtained.
The script is one of the apps available with Pi-Apps. First, install Pi-Apps from the terminal. Issue the command:
$ wget -qO- https://raw.githubusercontent.com/Botspot/pi-apps/master/install | bash
A shortcut is added to the desktop. Pi-Apps can also be launched from the start menu (it’s in the Accessories section), or from the command-line by using the command $ pi-apps
Here’s what you see when starting Pi-Apps.
Go to Tools and choose More RAM.
More RAM is a handy script that sets up ZRAM to increase the capacity of your RAM. ZRAM is a kernel module to compress data on-the-fly and store it in RAM. It has all sorts of uses, and works well with the Pi.
Click the Install button. The script is now installed on the system and creates a 15.8GB swapdrive.
We can verify the size of the swap from htop, top, or free. Here’s htop. Here’s my system. I’ve actually got a 17.8GB swap, consisting of the 15.8GB ZRAM swapdrive and the 2GB swap on the NVMe.
What’s good?
- Stops RAM-related freezes which can happen frequently with the Raspberry Pi models with 4GB or less RAM.
- Avoids wear on the device (SD card, USB, NVMe) previously used for swap.
- Uses the ZSTD compression algorithm making RAM usage more efficient. It can achieve a maximum 5:1 compression ratio although this can be significantly less.
- Makes changes to the kernel parameters to improve performance.
- Does not affect performance when the swapdrive is not used.
- Having no physical swap saves disk space.
- Very easy to uninstall.
What’s bad
- Unpredictability. Given that the compression ratio will vary, you don’t know the amount of effective swap.
- Compressing and decompressing RAM uses CPU cycles. I find that the performance hit is minimal with my workflow though.
- Doesn’t actually increase physical RAM. More physical RAM is always better.
Log file
To get an idea of what the install script does, I’ve reproduced part of the log file. This log file was generated on a Raspberry Pi 5 with 8GB RAM. With this machine, the script has setup 31.5 GB swapspace.
Disabling swap Created symlink /etc/systemd/system/dphys-swapfile.service → /dev/null. Checking system for compatibility... Done Creating zram script: /usr/bin/zram.sh Making it run on startup - Creating zram-swap.service - Reloading Systemd unit files - Enabling zram-swap.service to run on boot Created symlink /etc/systemd/system/multi-user.target.wants/zram-swap.service → /etc/systemd/system/zram-swap.service. Running it now. Output: Disabling zram... Done Setting up swapspace version 1, size = 31.5 GiB (33804763136 bytes) no label, UUID=d4b24c5a-38ed-4701-bacf-0420f73ae68a Setting up ZRAM-powered file storage at /zram mke2fs 1.47.0 (5-Feb-2023) Changing kernel parameters for better performance: vm.swappiness=100 ZRAM should now be set up. Consider rebooting your device. Below is a summary: NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram1 zstd 31.5G 132.8M 61.7K 720K 4 /zram /dev/zram0 zstd 31.5G 16K 59B 48K 4 [SWAP] You can see this at any time by running 'zramctl'
All articles in this series:
Raspberry Pi 5 Series | |
---|---|
iRasptek Starter Kit | All the kit you need to get started with the Pi 5 |
Pironman 5 Case Review | Transform the Pi 5 into a beautiful desktop mini PC |
Power Consumption | Compare the power consumption of the Pi 5 with Intel Mini PCs |
Increase Swap Memory Size | Increase the swap size from 512MB to 2GB |
ZRAM swapdrive | Simple script to use a ZRAM swapdrive instead of a swapfile |