WindowsMacSoftwareSettingsSecurityAndroidProductivityLinuxPerformanceAppleDevice Manageme.. All

How to Extend Swap Space on Ubuntu

Edited 2 weeks ago by ExtremeHow Editorial Team

Swap SpaceUbuntuMemoryLinuxConfigurationOperating SystemsPerformanceSystemAdministrationCommand Line

How to Extend Swap Space on Ubuntu

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.

Understanding swap space in Ubuntu

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.

Types of swap space

There are mainly two types of swap space in Ubuntu:

Checking current swap usage

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.

Expanding swap space on Ubuntu

In this section, we will discuss how to increase swap space on Ubuntu by increasing the size of the swap file or partition.

Method 1: Increasing swap space by adding a swap file

Adding a swap file is usually easier than modifying a partition. Follow these steps:

Step 1: Disable the current swap

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.

Step 2: Create new swap file

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.

Step 3: Secure the swap file

Change file permission for security:

sudo chmod 600 /swapfile

Step 4: Mark the file as swap

Now, convert this file into swap space:

sudo mkswap /swapfile

Step 5: Activate the swap file

To start using the swap file, activate it:

sudo swapon /swapfile

Step 6: Verify the swap

Make sure the new swap is active:

sudo swapon --show

You should see the newly added swap file in the output.

Step 7: Make the swap permanent

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

Method 2: Expanding swap space by enlarging the swap partition

Modifying the swap partition is more complicated and can be risky. Make sure you have proper backups before proceeding.

Step 1: Disable the swap partition

First, disable the swap partition:

sudo swapoff -a

Step 2: Modify partitions

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.

Step 3: Enable the swap partition

After resizing, reactivate your swap partition:

sudo swapon -a

Step 4: Verify and update 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.

Idea

Here are a few points to remember when expanding swap space:

Troubleshooting

If you experience problems after modifying the swap space:

Conclusion

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


Comments