Swap space, a swap partition or file, is a dedicated area on a computer’s hard drive or SSD used as virtual memory by the operating system, particularly in Linux systems. When the physical RAM (Random Access Memory) becomes insufficient to hold all the data that needs to be processed, the operating system transfers some of the less frequently used data from RAM to the swap space. This process is known as swapping or paging.
The importance of swap space size in Linux systems lies in its role as an extension of physical memory. With an adequately sized swap space, Linux can effectively handle situations where the demand for memory exceeds the available physical RAM.
Considering the importance of the Swap Space size, this post will demonstrate the essentials to increase the swap space in Linux. All the steps will be practically demonstrated on Ubuntu 22.04 as a reference.
How to Increase Swap Space in Linux?
The users have to determine how much swap space they want to increase. After that, check the current swap space. Then, create a Swap file or partition and update the “/etc/fstab” file to apply the changes. All these steps are practically demonstrated in this section:
Step 1: Check the Current Swap Space
Before proceeding with increasing the swap space, it’s essential to determine the current swap usage on your Linux system. Open a terminal and execute the following command:
$ sudo swapon --show
From the output, it is clear that our system currently has a swap space of “2.6GB,” and the file’s path is “/swapfile.”
Step 2: Create a Swap File or Partition
The next step involves creating a new swap file or partition to increase the swap space. Depending on your requirements and available resources, you can choose one of the following methods:
a) Creating a Swap File
To create a swap file, determine the desired size (e.g., 3GB) and execute the following commands one by one in the terminal:
- The first command will allocate the memory to the swap file.
- The second command sets the required permissions on the swap file.
- The third command will create the swap area on that file. The swap files’ name and location must be the same in all the commands, as in our case, it is “/newswapfile”:
$ sudo fallocate -l 3G /newswapfile $ sudo chmod 600 /newswapfile $ sudo mkswap /newswapfile
b) Creating a Swap Partition
If you prefer creating a swap partition instead, you can use tools like GParted or fdisk to allocate a new partition for swap space. Once the partition is created, format it as swap using the following command:
$ sudo mkswap /dev/partition_name
Replace “/dev/partition_name” with the device name of the newly created partition.
Step 3: Enable the Swap File or Partition
After creating the swap file or partition, you need to enable it to make it available. Execute the following command in the terminal:
$ sudo swapon /newswapfile
Or, if you created a swap partition, use the device name instead:
$ sudo swapon /dev/partition_name
This command activates the new swap space and adds it to the available swap areas.
Step 4: Update the /etc/fstab File
To ensure that the swap space remains active even after a system reboot, you need to update the /etc/fstab file. Open the file using a text editor with root privileges, such as:
$ sudo nano /etc/fstab
Add the following line at the end of the file:
$ /newswapfile none swap sw 0 0
Or, if you created a swap partition:
$ /dev/partition_name none swap sw 0 0
Save the changes and exit the text editor.
Step 5: Verify the Increased Swap Space
To verify that the swap space has been successfully increased, execute the following command:
$ sudo swapon --show
The output shows that the “/newswapfile” has a capacity of 3GB, which we set in the earlier steps. That’s how you can increase the swap space in Linux.
In Linux, Increasing swap space is essential for maintaining system performance and preventing memory-related issues. Following the step-by-step guide outlined in this article, you can successfully increase the swap space by creating a swap file or partition, enabling it, and updating the /etc/fstab file for persistence. It’s crucial to consider your system’s requirements and available resources when determining the appropriate size for the increased swap space. With an adequately sized swap space, your Linux system will be better equipped to handle memory demands and ensure smooth operation.
For more Tips and Tricks on Linux, visit and subscribe to Linux Genie.