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 lets you boot the Pi from a fast NVMe drive rather than an SD card.
The Raspberry Pi OS has a system service called dphys-swapfile that computes the size for an optimal swap file. It resizes an existing swap file if necessary. For the 4GB model the system service considers 512MB is the best swap file size. We can see its location with the command:
$ sudo swapon --show
I prefer to have a larger swap file. In the example below, I’m going to change the 512MB swap file to a 2GB swap file. This may be ‘sub-optimal’, but as I’m booting the Pi 5 from an NVMe drive (rather than an SD card), I’ve found the larger swap file helps in many situations. I don’t recommend you increase the swap file if you’re booting from an SD card. Note, while I found performance of the Pi improved and the system remained stable, your experience may differ. I don’t take any responsibility for any issues you may incur.
First, turn off the existing swap file.
$ sudo dphys-swapfile swapoff
Now disable the system service. If this step is omitted, you’ll find the swap file reverts back to 512MB after a reboot. I cannot think of any other Linux distribution that exhibits this behaviour.
$ sudo systemctl disable dphys-swapfile
Now delete the current 512MB swap file.
$ sudo rm /var/swap
Now I’ll allocate a 2GB swap file.
$ sudo fallocate -l 2G /var/swap
I need to set the permissions so only the root user can change the swap file.
$ sudo chmod 600 /var/swap
mkswap sets up a Linux swap area. Type:
$ sudo mkswap /var/swap
I can proceed and enable the swap file.
$ sudo swapon /var/swap
Make the change persistent with the command:
$ sudo bash -c "echo /var/swap none swap defaults 0 0 >> /etc/fstab"
Verify the change is persistent by rebooting the Pi.
As shown above, the swap is now 2GB. Job done!
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 |