The Swap space has a vital role in the system, especially on the Raspberry Pi due to minimalistic hardware in action. Learning how to manage it can help you save your system from a potential cripple at heavy usage.
What is Swap?
The Swap space is a straightforward concept: Linux systems divide their RAM into chunks called ‘pages.’ Every process that you run requires a certain amount of space in the RAM, and hence, a specific number of pages. ‘Swapping’ moves a process (generally a significantly less resource-demanding process) to a pre-configured space in the storage called the ‘Swap.’ This frees up pages on the RAM, allowing users to run bulkier and more applications.
This becomes especially important when working with systems with less RAM, which is usually the case with a Raspberry Pi. You will know that you need it if your Raspberry Pi system locks up often after a bit of usage or after opening up too many applications.
Managing Swap on Raspberry Pi
Thankfully, increasing or decreasing the Swap space on your Raspberry Pi isn’t a challenging task. First off, make sure that you have as few applications as possible because that way, you can ensure that the Swap isn’t already in use. Next, we have to turn the Swap off:
sudo dphys-swapfile swapoff
The next step is to modify the Swap configuration file. Enter into the command line:
sudo nano /etc/dphys-swapfile
Now that you have the file open, search for the term ‘swap size‘:
As you can see, the default Swap size is 100MB. Since this is in megabytes, change the value to whatever you want in MBs. For example, I will set it as half a GB, which is 512MB.
Now that you have configured the Swap size, save the file by pressing Ctrl-X and Y.
After this, we have to initialize the new Swap configuration:
sudo dphys-swapfile setup
And turn it back on:
sudo dphys-swapfile swapon
Finally, restart the system to be able to use the new Swap configuration:
sudo reboot
Conclusion
The Swap is a very innovative and efficient solution to memory allocation. In this case, where it is so necessary, managing Swap size can potentially make things a lot easier for all users. We hope that this article was helpful to you. Cheers!