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:
First thing to do it to turn off the swap file to avoid any issues:
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:
Search for the CONF_SWAPSIZE
variable and update it to 500
:
Save the file and exit the editor.
Now we will reinitialize the swap file setup using the following command:
Finally we will turn on the swap file:
Once this is done, we can reboot the system to make sure everything is working fine:
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! đź‘‹