In this guide, we will show you how to enable and increase the swap file on your Raspberry Pi.
The swap file is used to increase the system’s total accessible memory beyond its hardware capabilities.
This means that when all of the Raspberry Pi’s RAM is exhausted, it can start using the swap file as memory instead.
The addition of more virtual memory allows the system to deal with more memory intensive tasks without running into out of memory errors or having to shut down other additional packages.
However, the downside to this is that accessing the swap file is a significantly slower process that can create slowdowns.
The reason for this is that the swap file exists on your actual disk, which has significantly lower read and write speeds then your RAM.
Another caveat of a large swap file is that you need that space to be free on your SD Card. You can’t set a swap file on your Raspberry Pi larger than your available free space.
With the introduction of the Raspberry Pi 4, there has been less of a need to use a swap file due to the large amounts of RAM available.
Below is the equipment that we used for this guide on increasing the size of the swap on the Raspberry Pi.
Increasing the Swap File on a Raspberry Pi
In this section, we will be showing you the process of increasing the swap file on the Raspberry Pi.
Before beginning this section, make sure you aren’t running anything thats heavily using the memory. The reason for this is that it could potentially already be using the swap file that we are about.
1. Before we can increase our Raspberry Pi’s swap file, we must first temporarily stop it.
The swap file cannot be in use while we increase it.
To stop the operating system from using the current swap file, run the following command.
sudo dphys-swapfile swapoff
2. Next, we need to modify the swap file configuration file.
We can open this file using nano by using the command below.
sudo nano /etc/dphys-swapfile
3. Within this config file, find the following line of text.
You can use CTRL + W to search for text within the file.
To increase or decrease the swap file, all you need to do is modify the numerical value you find here.
This number is the size of the swap in megabytes.
For example, if we wanted to increase our swap size to 1GB, we would change that line to the following.
Whatever size you set, you must have that space available on your SD card.
4. Once you have made the change, save the file by pressing CTRL + X, followed by Y, then ENTER.
5. We can now re-initialize the Raspberry Pi’s swap file by running the command below.
Running this command will delete the original swap file and recreate it to fit the newly defined size.
sudo dphys-swapfile setup
6. With the swap now recreated to the newly defined size, we can now turn the swap back on.
To start the operating systems swap system, run the following command.
sudo dphys-swapfile swapon
While the new swapfile is now switched on, programs will not know that this new memory exists until they restart.
7. If you want all programs to be reloaded with access to the new memory pool, then the easiest way is to restart your device.
To restart your Raspberry Pi, all you need to do is run the command below.
We hope you have successfully learned how to increase and decrease the swap file on your Raspberry Pi.
If you have run into any issues with this guide, feel free to leave a comment below.