Are you dealing with low RAM on your Ubuntu system? This is a common issue on systems required more memory to running applications smoothly. This can be resolved by adding more memory, but that involves additional costs. If you don’t want to spend extra money, you can easily create a swap file and add it to your system to avoid low memory problems. If you are facing issue first time you can also try clearing the cached memory.
What is Swap Space
A swap file in Linux is the space allocated on disk utilized by system on low RAM. When RAM fills up, inactive data gets moved to this file on your disk, freeing up RAM for important processes. It’s like using extra space on your desk when your main workspace gets cluttered.
The Swap space helps your system for:
- Performance: On systems with limited RAM, swap space can prevent the system from crashing under heavy load.
- Flexibility: Easily adjust the amount of virtual memory available without needing to physically add RAM.
Prerequisites
- A system running Ubuntu 24.04
- Sufficient disk space for the desired swap file size
- Root or sudo privileges
Step 1: Checking the System for Existing Swap Space
Before adding a new swap file, it’s good practice to check whether your system already has some swap configured. Linux offers a few tools to see the swap details. Try swapon command for active swap details, or free to see total and used swap space
sudo swapon --show
The command will return nothing in case of no swap added to system.
Step 2: Creating a Swap File
- Choose the Size: First of all, you need to decided the size of swap space required on your system. Generally it is recommended to equal size or double the amount of RAM, depending on your system usage and available disk space.
- Create the File: You can use fallocate command to create a swap file with your desired size. For example, to create a 4GB swap file, execute:
sudo fallocate -l 4G /swapfile
- Set Proper Permissions: Set the correct permissions on your swap file to ensure it’s not readable by anyone except the root user:
sudo chmod 600 /swapfile
Step 3: Enabling the Swap File
- After creating the swap file, we need to format it as Swap:
sudo mkswap /swapfile
- Now, you can enable the Swap file:
sudo swapon /swapfile
- Verify the swap is active on your system:
sudo swapon --show
This command should now show your newly created swap file.
Step 4: Making the Swap File Permanent
You have already enabled swap memory on your system. However, you also need to make sure that the swap file turns on automatically when your system starts. To do this, add it to the system’s /etc/fstab file.
- Edit /etc/fstab file in your favorite text editor:
sudo nano /etc/fstab
- Append the following line to /etc/fstab:
/swapfile none swap sw 0 0
- Save and close the file.
Step 5: Adjusting Swappiness
The swappiness parameter controls how often your system uses swap space. You can adjust it to tune your system’s performance with respect to RAM and swap usage.
- Check Current Swappiness Value:
cat /proc/sys/vm/swappiness
- Change Swappiness Value (e.g., to 20, which is a recommended value for desktop systems):
sudo sysctl vm.swappiness=20
- Make It Permanent by adding
vm.swappiness=20
to/etc/sysctl.conf
.
Conclusion
You’ve successfully added and configured a swap file on your Ubuntu 24.04 system. This addition should help in managing your system’s resources more effectively, especially under heavy load. Remember, while swap can help in managing memory pressure, it’s not a substitute for adding more physical RAM when consistently facing performance issues.