Swap (swap space) is an important mechanism used in Linux systems to extend physical memory, when there is not enough physical memory, the system will swap inactive memory pages into Swap space. The following is the complete procedure for configuring Swap in Ubuntu:
1. Check the current Swap status
First verify that Swap is enabled on your system:
sudo swapon --show
- no output: Indicates that there is currently no active Swap space
- exporting: Display information about enabled Swap partitions/files
2. Creating a Swap file
2.1 Creating a Swap File
Recommended fallocate Quick Create (use if not available) dd):
# Method 1: Use fallocate (recommended, faster)
sudo fallocate -l 2G /swapfile # Creating a 2GB Swap file
# Method 2: Use dd (better compatibility)
sudo dd if=/dev/zero of=/swapfile bs=1G count=2 status=progress
2.2 Setting file permissions
Make sure that only the root user has access:
sudo chmod 600 /swapfile
2.3 Formatting as a Swap Region
sudo mkswap /swapfile
Example output:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=xxxx-xxxx-xxxx-xxxx-xxxx
2.4 Enabling Swap Files
sudo swapon /swapfile
3. Verify Swap enabled status
sudo swapon --show
Example output:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
or using free-h View:
free-h
Example output:
total used free shared buff/cache availableMem: 3.8G 1.2G 100M 200M 2.5G 2.2GSwap: 2.0G 0B 2.0G
4. Persistent configurations (power-on auto-mount)
4.1 Editing an fstab file
sudo nano /etc/fstab
4.2 Adding Swap Configuration Items
Add at the end of the document:
/swapfile none swap sw 0 0
or configuration with prioritization (optional):
/swapfile none swap sw,pri=10 0 0
pri=10: Sets the Swap priority (the higher the value the higher the priority)- Save to exit:
Ctrl+X→Y→Enter
5. Configure Swappiness parameters (optional)
The Swappiness value controls the system's tendency to use Swap:
- 0-100: The smaller the value, the more you tend to use physical memory.
- default value:: 60
5.1 Viewing Current Values
cat /proc/sys/vm/swappiness
5.2 Temporary modifications (reboot disabled)
sudo sysctl vm.swappiness=10
5.3 Permanent modifications
Edit the configuration file:
sudo nano /etc/sysctl.conf
Add at the end of the document:
vm.swappiness=10
Apply the configuration after saving the exit:
sudo sysctl -p
6. Cautions
- File Size Selection::
- Physical Memory ≤ 2GB: recommended Swap = 2 × Physical Memory
- Physical Memory 2-8GB: recommended Swap = Physical Memory
- Physical memory ≥ 8GB: recommended Swap = 4-8GB (or as needed)
- Performance considerations::
- Swap files underperform Swap partitions
- Swap on SSDs is faster than HDDs
- Consider using
zswapmaybezramImprove performance
- Secure deletion of Swap(if required):
sudo swapoff /swapfilesudo rm /swapfilesudo nano /etc/fstab # Delete corresponding line - Monitoring Swap Usage::
vmstat 1 5 # Refresh every second, 5 times in totaltop # Press M to sort by memoryhtop # More intuitive monitoring tool (installation required)
By following these steps, you can successfully configure and enable Swap space on your Ubuntu system and adjust system parameters as needed for optimal performance.