Edited 2 weeks ago by ExtremeHow Editorial Team
Swap SpaceUbuntuMemoryLinuxConfigurationOperating SystemsPerformanceSystemAdministrationCommand Line
This content is available in 7 different language
In Ubuntu, swap space plays a vital role in managing memory. It acts as an overflow when your physical RAM is fully utilized. Swap space can be a dedicated area on your hard drive where the operating system can temporarily store data that is not being actively used. If your system frequently runs out of RAM and starts swapping too often, which can slow down the system, increasing swap space can be beneficial. This guide will cover how to increase swap space on Ubuntu systems.
Swap space in Ubuntu is used to increase the amount of available memory by dedicating additional disk space to act as virtual memory. Normally, Linux users configure swap space during installation, but sometimes it may be necessary to increase this space after installation, usually when you face a memory shortage.
There are mainly two types of swap space in Ubuntu:
Before increasing the swap space, it is necessary to check the current swap usage. You can do this using the command:
sudo swapon --show
This command will display the current swap usage. It is advisable to have some swap space, however too much swap space can decrease performance due to the slower speed of the hard drive compared to RAM.
In this section, we will discuss how to increase swap space on Ubuntu by increasing the size of the swap file or partition.
Adding a swap file is usually easier than modifying a partition. Follow these steps:
sudo swapoff -v /swapfile
Replace /swapfile
with the path to your current swap file if it is different. This command disables the swap file so you can safely modify it.
Here, we are going to create an additional swap file at /swapfile
. The size of this file will determine how much swap space will be added. Replace 1G
with your desired swap space:
sudo fallocate -l 1G /swapfile
If fallocate
fails or is unavailable, you can use dd
:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
The above example creates a 1GB swap file. Adjust count=1024
as needed to allocate the desired size.
Change file permission for security:
sudo chmod 600 /swapfile
Now, convert this file into swap space:
sudo mkswap /swapfile
To start using the swap file, activate it:
sudo swapon /swapfile
Make sure the new swap is active:
sudo swapon --show
You should see the newly added swap file in the output.
Edit the /etc/fstab
file to automatically enable the swap file at boot:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Modifying the swap partition is more complicated and can be risky. Make sure you have proper backups before proceeding.
First, disable the swap partition:
sudo swapoff -a
Use a partition editor such as GParted to increase the size of your swap partition. Open GParted as follows:
sudo gparted
In GParted, right-click on your swap partition and resize it to the desired size. You will need free space next to the swap partition for this operation.
After resizing, reactivate your swap partition:
sudo swapon -a
fstab
Make sure your swap partition is active and registered in /etc/fstab
:
sudo swapon --show
Check that /etc/fstab
has an entry for your partition. If necessary, edit it to reflect any partition UUID changes.
Here are a few points to remember when expanding swap space:
If you experience problems after modifying the swap space:
/etc/fstab
: Make sure there are no syntax errors in the swap entry.sudo swapon --show
to confirm swap activity.Increasing swap space in Ubuntu is a straightforward process that can help improve system performance when physical memory is insufficient. Whether using a swap file or a partition, the main thing is to plan the size carefully and be mindful of the potential impacts on system performance. By following these steps, you can efficiently manage and increase swap space on your Ubuntu system.
If you find anything wrong with the article content, you can