How to Add Swap Memory on Ubuntu for Enhanced Performance

Swap memory plays a crucial role in improving the performance of your Ubuntu system, especially during memory-intensive tasks. It acts as an extension of your physical RAM, providing a space on your disk where data can be moved when the RAM is fully utilized. Adding swap memory is a quick way to boost system performance without upgrading hardware. In this guide, we will walk you through the process of adding swap memory on Ubuntu using a 10GB swap file.

What is Swap Memory?
Swap memory allows your operating system to temporarily move less-used data from the RAM to a specific space on the hard disk, called a swap file or swap partition. When your system runs out of physical memory (RAM), it uses swap space to prevent programs from crashing or slowing down. Although swap memory is much slower than RAM, it ensures that your system remains stable under heavy memory load.
Adding swap is particularly useful for systems with limited physical memory that need to handle memory-heavy applications like software development environments, large datasets, and server tasks.
Step-by-Step Guide to Add Swap Memory on Ubuntu
Step-by-Step Guide to Add Swap Memory on Ubuntu
- Turn Off Existing Swap Memory (Optional)
First, if you have existing swap memory, you need to turn it off before making any changes. This ensures a smooth configuration. To do this, run the following command in your terminal:
sudo swapoff -a
- 2. Allocate Swap File
Next, we will create a new swap file using the fallocate
command. This command allocates the space needed for the swap file (in this case, 10GB). Here’s the command to allocate a 10GB swap file:
sudo fallocate -l 10G /swapfile
The fallocate
command quickly creates a file by preallocating the disk space, so no disk write occurs during file creation.
- Set Permissions
After creating the swap file, it’s crucial to secure it by setting the correct permissions. This ensures that only the root user can read or write to the file, preventing unauthorized access.
sudo chmod 600 /swapfile
- Set Up the Swap Space
Now that the swap file has been created and secured, it’s time to tell the system to use this file as swap space. The mkswap
command is used to set up the file as swap space:
sudo mkswap /swapfile
- Enable Swap Memory
After the swap space is configured, we can activate it using the following command:
sudo swapon /swapfile
Your new swap space is now active, and your system can use it as an extension of the physical memory.
- Make the Swap Permanent
By default, the swap file will be deactivated upon system reboot. To ensure that the swap space persists after a reboot, we need to add it to the /etc/fstab
file.
First, make a backup of the fstab
file:
sudo cp /etc/fstab /etc/fstab.bak
Then, add the following line to the file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
This entry tells the system to automatically mount the swap file at boot time.
Here is the link for the whole code at once
Run this script for adding swap memory on Ubuntu system
Verify the Swap Configuration
To confirm that the swap file is active and being used by the system, you can use the following command:
sudo swapon --show
This command will display the swap files that are currently in use. You can also check the overall memory and swap usage with:
free -h
This shows how much swap memory is in use and how much is free.
Conclusion
Adding swap memory on Ubuntu is a simple yet effective way to improve your system’s performance, especially when dealing with memory-intensive tasks. By following the steps outlined above, you can easily allocate additional swap space and ensure that your system remains stable, even under heavy memory load. Whether you’re running servers, developing software, or using resource-heavy applications, swap memory can provide the necessary buffer to keep your system running smoothly.