October 16, 2024

How to increase Swap Memory Size in Raspberry Pi 5

In this tutorial, we explain how to increase the swap memory file size in Raspberry Pi 5. The YouTube video tutorial is given below.

Motivation and Important Notes

Raspberry Pi 5 comes in two versions: a version with 4GB RAM and a version with 8GB RAM. However, if you run a number of applications or processes at the same time, this memory might quickly be consumed, and your Raspberry Pi might slow down or experience temporary freezes. Loosely speaking, swap memory will “virtually increase” RAM size. On the other hand, it will occupy some extra space on your drive, such as a micro-SD, SSD, or NVMe SSD. It will be used in addition to the regular RAM memory, and especially when the RAM memory is totally consumed. That is, applications can store their data and processes in the swap memory. Larger swap memory is extremely useful when you have a large number of applications that are running concurrently since when the regular RAM is consumed, swap memory can be used as a “virtual” RAM memory. That is, by increasing the swap memory size we can improve the stability of the system and prevent some applications from crashing.

Disclaimer: We do not take any responsibility if during the process of increasing the swap memory size or after increasing the swap memory size your system or some applications experience issues. In our case, the system remained stable and its performance improved.

Also, keep in mind that swap memory is actually a physical memory space reserved on your drive, such as a micro-SD, SSD, or NVMe SSD. By increasing the swap memory size, we allow applications to read and write data to our disk. In some scenarios, read and write operations might happen with a large frequency. Some types of drives, such as micro-SD are not suitable for high-frequency read and write operations. First of all, they are slow, and secondly, their lifetime might significantly be decreased if we quickly and many times perform read and write operations. SSDs and NVMe SSDs are more suitable for high frequency read and write and they are more durable than micro-SDs.

How to increase Swap Memory Size in Raspberry Pi 5

To increase the swap file size, perform the following steps. Show the current swap partitions and memory:

sudo swapon --show

Before we can make any changes, we need to disable the swap memory:

sudo swapoff -a

We need to erase the current swap file:

sudo rm /swapfile

The following code line will change the swap memory size to 10G (I have enough space on my SSD and I can do that):

sudo fallocate -l 10G /swapfile

Next, set the permissions such that only the root user can change the swap file size:

sudo chmod 600 /swapfile

To implement the changes, type this

sudo mkswap /swapfile

Finally, you need to enable back the swap memory and swap file:

sudo swapon /swapfile

Restart the computer, and overclocking and swap file changes will be implemented. Then, open a terminal to check the status:

sudo swapon --show

Also, you can check the swap memory size, by opening a terminal and by running the program called “top”:

top