Image of the Raspberry Pi 5

Raspberry Pi 5 Desktop Mini PC: Increase Swap Memory Size

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

Swap location

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

Turn off swap file and system service

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"

Steps to create 2GB swap file and make it permanent

Verify the change is persistent by rebooting the Pi.

Now we have 2GB swap

As shown above, the swap is now 2GB. Job done!


All articles in this series:

Raspberry Pi 5 Series
iRasptek Starter KitAll the kit you need to get started with the Pi 5
Pironman 5 Case ReviewTransform the Pi 5 into a beautiful desktop mini PC
Power ConsumptionCompare the power consumption of the Pi 5 with Intel Mini PCs
Increase Swap Memory SizeIncrease the swap size from 512MB to 2GB
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Please read our Comment FAQ before posting.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments