Swap Space in Linux: How To Increase Swap Space In Linux


Table of Contents

So it has been long since you started using Linux. Now that you’re enjoying what Linux has to offer, one of the things you often come across is the Swap Space.

In this article, I will discuss Swap Space in Linux, its importance, when it’s required, and how beneficial it is. I will also discuss how we can set up and manage swap space in Linux.

What is Swap Space in Linux?

Simply swap space is the disk storage that the operating system uses when it runs out of physical memory, i.e. RAM. When the computer is running multiple processes and the system does not have enough RAM to support them all, the operating system moves the least-used data from the RAM to the swap freeing up enough space to keep more intensive processes running.

It acts like the physical memory. It can prevent the computer from crashing, and can prevent various malfunctions.

Benefits of Swap Space

  • Enables better multitasking: Swap space can allow the user to run more applications simultaneously and prevent system from running out of memory.
  • Prevents the system from crashing when it runs out of RAM.
  • Assist in running memory-intensive applications such as video editors and graphic design apps.
  • Can keep the system running smooth when the memory usage is high. The system can quickly transfer least-used data from the RAM.

​Do I really need Swap space?

Not always, the provided system has a large amount of physical memory (RAM). But it is recommended to have swap space handy. The system may crash when the system is run out of physical memory when many applications are running with large memory footprint. When compared to RAM, disk space is relatively cheap.

Swap space as Partition or file?

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of both. By default, most of the Linux distributions create a dedicated swap partition or a file on the system partition during installation. Windows operating system generally has the swap space as a file.

Though there is no hard and fast rule to have swap space, it is recommended to have at least 1.5 times the physical memory. In the case of hibernation, the swap partition should be at least as big as the RAM size.

Increase swap space in Linux

Login as root.

sudo su
get superuser ubuntu linux

Create swap file in directory “/var” with name “swapfile”.  At the shell, create the file and set root permissions as follows:

cd /var
touch swapfile
chmod 600 swapfile
ls -la swapfile
create swap file

Use “dd” command to fill the swap file with 1 GB size (as an example) as follows –

dd if=/dev/zero of=/var/swapfile bs=1024k count=1000
create swap file with data

Now setup the swap file –

mkswap /var/swapfile

Enable the swap file –

swapon /var/swapfile
enable swap file

To check whether the new swap file was successfully created, either of the below commands can be used.​

# cat /proc/swaps
# swapon –show 
get all swap files

Add below line to the “/etc/fstab” file so that next time when the system boots, it enables the newly created swap file –

/var/swapfile none swap sw 0 0

Disable and remove a swap file

Disable the swap file.

# swapoff /var/swapfile 

Delete the swap file.

# rm /var/swapfile

Remove the entry from “/etc/fstab” file.

/var/swapfile none swap sw 0 0   


The swapping mechanism does have a downside. Because resides in hard disks, the access time for swap is slower, and hence it cannot be a complete replacement for the physical memory.


System administrators can benefit greatly by adding sufficient swap space to keep the system running smoothly. Regular monitoring of system memory usage helps in determining the size of the swap space.

If you need more information, you can also refer to the Red Hat chapter on the subject.

Frequently Asked Questions

How is swap space calculated in Linux?

In Linux, you can check the current swap space usage and availability using the free command, which displays the amount of physical and swap memory available in the system. You can also use the swapon command to add, enable or disable swap space on the system.

How much swap space is needed in Linux?

Generally, the amount of swap space is calculated based on the following guidelines –

Rule of thumb: Allocate swap space twice the amount of RAM installed on the system.

For systems with less than 2GB of RAM: Allocate swap space equal to the amount of RAM installed.

For systems with more than 2GB of RAM: Allocate swap space 1.5 times the amount of RAM installed on the system.

However, these guidelines are not strict rules, and the optimal amount of swap space can vary depending on the specific usage of the system. It is important to monitor the system’s memory usage and adjust the amount of swap space accordingly.

What to do if swap space is full in Linux?

If the swap space is full in Linux, the system’s physical memory (RAM) is also fully utilized, and the system is running out of memory. This can cause the system to become unresponsive or even crash. Here are some steps you can take to resolve the issue –

Identify the processes consuming the most memory: Use the top or htop command to identify the processes consuming the most. Once you have identified the processes, you can try to kill them or reduce their memory usage.

Increase the swap space: If the current swap space is insufficient, you can increase it by creating a new swap file or partition. You can use the mkswap command to create a new swap file or fdisk to create a new swap partition. After creating the new swap space, you can enable it using the swapon command.

Reduce memory usage: You can reduce memory usage by stopping unnecessary services or processes, disabling graphical effects, reducing the size of browser caches, or using lightweight applications.

Add more RAM: If the system frequently runs out of memory, adding more RAM can help resolve the issue.

In some cases, it may be necessary to reboot the system to free up memory and clear the swap space. However, this should be a last resort, and you should resolve the issue without rebooting if possible.

Credit to – Ramakrishna Rujure

Linux TutorialsUncategorizedhow-to