Swap Memory Management
12/01/2025
Why use Swap Memory?
Swap allows the system to use disk space as virtual RAM when physical memory is fully used, helping handle memory-intensive tasks and preventing system crashes. Howver, swap i slower than RAM due to disk access speeds.
Good Swap Size practices
A general rule is to set swap memory to TWICE the size of your RAM. For example:
- 512MB RAM -> 1GB swap
- 1GB RAM -> 2GB swap
- 4GB RAM -> 8GB swap
Managing Swap on Linux
-
Disable current Swap
sudo dphys-swapfile swapoff -
Open the Swap file
sudo nano /etc/dphys-swapfile -
Locate the line below and modify it to the desired size (in MB):
CONF_SWAPSIZE=1024 # Example for 1GB Swap -
Recreate the Swap file
sudo dphys-swapfile setup -
Re-enable Swap
sudo dphys-swapfile swapon -
Option, but recommended - Reboot
sudo reboot -
Verify the Swap with monitoring tools like htop
htop
Conclusion
Proper swap memory management helps your Linux system handle resource-intensive tasks more efficiently. While swap can improve performance, finding the right balance is key to avoiding slowdowns or unnecessary storage wear. By optimizing swap settings, you ensure better system stability, responsiveness, and longevity.