Increase Swap Memory on Raspberry Pi

📆 · ⏳ 3 min read · ·

Introduction

Swap file is a file on the disk that can be used to extend the RAM on your system. It is used when the RAM is full and your system needs more memory resources. It is also used to store the data that is not accessed frequently.

This is especially useful on Raspberry Pi with limited amount of RAM. For example I have a Raspberry Pi Zero 2W which has 512MB of RAM. When I started adding more services on it I could see with htop that the RAM is becoming almost full. This is where the swap file comes in handy.

The default swap file size on Raspberry Pi is 100MB. This is might be enough for some use cases but if you need more you can increase it.

Let’s see how to do it.

Increase Swap Memory

We will use the dphys-swapfile package to increase the swap memory. This package is installed by default on Raspberry Pi OS. If you don’t have it installed you can install it with the following command:

Terminal window
sudo apt install dphys-swapfile

First thing to do it to turn off the swap file to avoid any issues:

Terminal window
sudo dphys-swapfile swapoff

Next we will update the CONF_SWAPSIZE variable in the /etc/dphys-swapfile file. This variable is used to set the size of the swap file. The default value is 100MB. We will set it to 500MB

I am using vi to edit the file but you can use any editor you like:

Terminal window
sudo vi /etc/dphys-swapfile

Search for the CONF_SWAPSIZE variable and update it to 500:

CONF_SWAPSIZE=500

Save the file and exit the editor.

Now we will reinitialize the swap file setup using the following command:

Terminal window
sudo dphys-swapfile setup

Finally we will turn on the swap file:

Terminal window
sudo dphys-swapfile swapon

Once this is done, we can reboot the system to make sure everything is working fine:

Terminal window
sudo reboot

After the reboot you can check the swap file size using the htop command and check for the Swp value, it should be 500M.

đź’ˇ

One thing to note is if you want to increase the swap file size to more than 2GB then you would need to uncomment the CONF_MAXSWAP variable in the /etc/dphys-swapfile file and set it to the desired value.

I have personally never needed it but if you do then you can do it (not verified by me).

Conclusion

In this post we saw how to increase the swap memory on Raspberry Pi. This can be useful if you are running out of RAM on your Raspberry Pi.

I hope you liked this post. If you have any questions or feedback reach out to me on X / Twitter ↗️.

See you in the next post. Until then, keep HomeLabbing! đź‘‹

You may also like

  • Resolving Missing Memory Stats in Docker Stats on Raspberry Pi

    Running Docker containers on your Raspberry Pi can be a great way to explore different software and services. However, encountering issues with retrieving memory usage information using docker stats can be frustrating. In this blog post will dive into the common reasons behind missing memory stats and guide you through the troubleshooting steps to resolve them.

  • Fix Sudo not asking Password on Raspberry Pi

    If you have installed a fresh copy of Debian or Raspbian on your Raspberry Pi you might notice that sudo does not ask for a password. This is a security risk and should be fixed. Let's see how to fix it.

  • Update Wifi Password on Raspberry Pi in Headless Mode

    Changing the wifi password after the first installation can be tricky sometimes. Here is a quick guide on how to do it quickly and easily.