3 min read

Maximizing Raspberry Pi Performance: A Guide to Increasing Swap Size

Raspberry Pi 3B

The Raspberry Pi is one of the most popular and widely used single-board computers. Due to its compact size and affordable cost, it is used in various projects ranging from home servers to using it as an IOT device. Despite its impressive capabilities, Raspberry Pi might sometimes take a hit in its performance.

During my brief usage in expanding my Home Labbing capabilities, I've deployed many of the services in my Raspberry Pi 3B. At times, most of the services crashed because the Pi was out of memory. The Raspberry Pi 3B which I have comes with only 1GB RAM. One of the things that came to my mind to overcome this was to increase its swap size.

Swap Size is nothing but a virtual memory. When the system's physical RAM is fully occupied and is exhausted, the operating system can use swap space to temporarily move data from RAM to free up memory for other tasks. This virtual memory is nothing but your storage space. So the swap size cannot exceed your available storage space.

In this article, let us look into how to increase the swap size of a Raspberry Pi. It might not be needed in the case of new variants of Raspberry Pi as it comes with more RAM by default.

Turn off all the services or processes before proceeding further as the Pi may already be using the swap and we need to turn the swap off to make changes

Increasing the Swap File:

  • Login to the Raspberry Pi user account and open a terminal. Type in the below command to turn the swap off.
sudo dphys-swapfile swapoff
  • We now need to modify the swap configuration. Type in the below command to open the configuration file
sudo nano /etc/dphys-swapfile
  • Now we need to search for a particular line in which the current swap size is mentioned. It would look like this "CONF_SWAPSIZE=100". Now replace this 100 with the size that you want. The value of the size is given in MB (Mega Bytes).
Swap Size Configuration
  • We can now save the file by pressing the Ctrl + X key on the keyboard. It would ask if we need to save the file. We can hit Y followed by the Enter key on the keyboard to save the configuration file.
  • Now we need to reinitialize the swap file. Type in the below command so that the original swap file will be recreated with the new size that we just configured.
sudo dphys-swapfile setup
  • We can now turn the swap back on.
sudo dphys-swapfile swapon
  • In order for the changes to be reflected, we need to reboot the Raspberry Pi once.
sudo reboot
  • After rebooting, we can now check the current swap size using the below command.
free -h
Free RAM and Swap

And Viola! We have successfully increased the size of the swap in our Raspberry Pi. Using this simple trick we can increase the performance of our Raspberry Pi. It might not be the best trick but it is a game-changer for ensuring a smooth operation and getting a slight memory boost for memory-intensive operations.

Stay tuned for more detailed instructions on expanding our Home Labbing capabilities.

Happy Homelabbing!!!