2 Ways to create or extend Swap space in Linux

Users can create a swap space during the installation of any Linux operating system, which is essential. If you forgot to do it, or would like to do it later, you can do so any time.

Sometimes you may need to add more swap space when upgrading the RAM after installing the operating system.

For example: When you upgrade the RAM from 1GB to 2GB, the swap should be upgraded from 2GB to 4GB because as a general rule of thumb, the swap space must be double the amount of physical RAM up until the RAM size is 2GB.

It’s recommended to create a dedicated swap partition, but if you don’t have a free partition then use a swap file, or a combination of it.

Swap space is generally recommended to have a minimum of 4GB, but this can vary depending on your application requirement and the environment.

What is swap space?

Swap is a space on the disk which is reserved to be used as a virtual memory when the amount of physical memory (~RAM – Random access memory) is full.

If the system needs more memory resources when the RAM is full, inactive pages in memory are moved to the swap space which can help the system to run the applications for some more time but it should not be considered a replacement for more RAM.

How to check current swap size on Linux

Let’s first find out the size of existing swap space using free command &/or swapon command. As per the following output, the current swap space is '2GB :

free -h
              total        used        free      shared  buff/cache   available
Mem:           2.0G        1.3G        139M         45M        483M        426M
Swap:          2.0G        655M        1.4G

sudo swapon --show
NAME      TYPE      SIZE   USED PRIO
/dev/sda3 partition   2G 655.2M   -1

Now let’s proceed to understand how to create swap partition:

Method-1: Creating a Swap partition

Hard drive partition is one of the recommended methods to create a swap space.

If you have an additional hard disk, create the new partition using fdisk command. Let us assume that we have created the partition called “/dev/sda4”.

Use ‘mkswap’ command to convert the partition into swap area as shown below:

$ sudo mkswap /dev/sda4

Enable the swap file by running below command:

$ sudo swapon /dev/sda4

Add newly created swap file into fstab file, so that swap space partition is available even after the reboot:

$ vi /etc/fstab

/dev/sda4  swap  swap  defaults  0 0

Check the newly created swap space:

$ swapon --show
NAME       TYPE       SIZE USED PRIO
/dev/sda3  partition    2G 1.3G   -1
/dev/sda4  partition    1G   0B   -2

Now you can see the new 1GB /dev/sda4 swap partition. Reboot the system to use the new swap partition.

Alternatively, you can create swap space using the LVM partition, which allows you to extent the swap space whenever you need it.

Removing a Swap partition

Refer to the following three steps to deactivate and remove the swap partition.

1) Deactivate the swap space by running the below command:

$ sudo swapoff -v /dev/sda4

2) Next, remove the swap file entry from the ‘/etc/fstab’ file:

/dev/sda4  swap  swap  defaults  0 0

3) Finally, remove the partition using the fdisk command:

$ sudo fdisk /dev/sda
Command (m for help): p
Command (m for help): d
Partition number (1-4): 4
Command (m for help): w

Method-2 : Creating a Swap file

When you don’t have an additional partition, the swap space can be created using swap file, which can be done in a number of ways, but I prefer to go with the dd command.

To do so, you need to create a file of a certain size on Linux system. dd command is a utility, which helps you to create a file with a pre-allocated size instantly.

The following dd command will create 1GB of /swapfile :

$ sudo dd if=/dev/zero of=/swapfile1 bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 16.6154 s, 64.6 MB/s

Details :

  • if=/dev/zero is a input file, /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it.
  • of=/swapfile1 is an output file
  • bs=1G : Read and write up to 1GB at a time
  • count=1 : Copy only 1 BLOCKS of input blocks.

Check whether it has created correct sized file or not:

$ ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Jun  7 09:58 /swapfile

Change the file permission to 600, to make it only accessible by the root user as shown below:

$ sudo chmod 600 /swapfile

Convert the file as a swap file by running the following command:

$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=96def6d7-b2da-4954-aa72-aa32316ec993

Enable the swap file by running below command.

$ sudo swapon /swapfile

Add newly created swap file into the ‘fstab’ file, so that swap space partition is available even after the reboot:

$ vi /etc/fstab

/swapfile  swap  swap  defaults  0 0

Check newly created swap file:

$ swapon --show
NAME       TYPE       SIZE USED PRIO
/dev/sda5  partition    2G 1.3G   -1
/dev/sda4  partition    1G   0B   -2
/swapfile  file      1024M   0B   -3

Now you can see the new 1GB swap file. Reboot the system to use the new swap file.

Removing a Swap file

Refer to the following three steps to deactivate and remove the swap file.

1) Deactivating the swap space by running the below command:

$ sudo swapoff -v /swapfile

2) Next, remove the swap file entry from the ‘/etc/fstab’ file:

/swapfile  swap  swap  defaults  0 0

3) Finally, remove the actual ‘swapfile’ file using the rm command:

$ sudo rm /swapfile

Conclusion

In this article, you learnt how to create, activate & remove a swap file and the partition on Linux.

If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!

4 Comments on “2 Ways to create or extend Swap space in Linux”

  1. Great article.. thank you… just when I created a new swapfile with the new space 512GiB it created it as 40GB.

    sudo dd if=/dev/zero of=/swapfile1 bs=512G count=1
    dd: memory exhausted by input buffer of size 549755813888 bytes (512 GiB)

    1. Use smaller value for bs= parameter and larger value for count= parameter to avoid this issue. For instance,
      sudo dd if=/dev/zero of=/swapfile1 bs=1M count=512000

Leave a Reply

Your email address will not be published. Required fields are marked *