How to check and add more SWAP space(memory) in Ubuntu?

  11 months ago     Views:  693  
Jitesh Meniya
2 Follows


A computer has limited amount of physical memory, So when your computer run out of physical RAM, then computer need more space, where inactive data is stored in virtual memory on disk.

Swap space or Swap memory is the virtual memory(RAM) on Hard disk. Swap space is useful when applications that aren't being used can be stored on disk until they are used. While Swap space is not in memory use, it is also used as Disk space. Swap space is useful in adding more RAM, it should be noted that reading memory from disk is slower than reading memory from RAM. So if you need more RAM, best option is to increase RAM in the system, but if you plans to add more RAM later, then you can add more Swap space.

Check Swap space

To add the Swap space, first you need to check how much Swap space is already in the system. Generally there are half of the RAM, Swap space is needed. Run the following command in the Terminal window to check current Swap space in the system.

sudo swapon -s

If you already have Swap sapce, then you will get bellow output otherwise you only get headings:

Filename                Type        Size    Used    Priority
/dev/sda5               partition    999420    0        -2

Or run the bellow command to get in more details.

sudo free -h

And you will see information similar to bellow details:

              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.9G        2.9G        535M        1.9G        3.9G
Swap:          975M          0B        975M

Add more Swap space

Currently there is no Swap space available in my ubuntu system. Suppose now I want to create 1GB of Swap space in the system.

There are generally 2 methods to create Swap space.

1. Using fdisk command

First turn off Swap partition if it will be in use.

sudo swapoff -a

Now we need to create new partition on Disk. I will create Swap space on sda drive. Run the bellow command and it will enter to disk partition menu to create new partition.

sudo fdisk /dev/sda

Press n to create new partition. Now you will asked to enter first cylinder value. Press enter to use default value or enter first cylinder value and then hit enter. Now enter last cylinder value. You can use any format to enter Cylinder value, for example 1000M or 1GB of space. This is the disk we are using for Swap space, so type 1GB and hit enter.

We have created disk partition, now we are changing type of partition. For that type t and it will ask number for the disk partiotion, which is in most cases it will be 5. Now type partititon type id, for us 82 id is for Linux swap / Solaris. Type L to get all partition type id. So type 82 and it will change disk partition to Linux swap / Solaris.

Now type w to save and exit. Now you are back to the command line.

We have successfully created disk partition. Now we need to modify  /etc/fstab to mount new Swap space.

First check if you have created right disk partition

sudo fdisk -l

Run the mkswap command with partition disk

sudo mkswap /dev/sda5

Now modify /etc/fstab file and add new partition in the list using Vim or Nano Editor. It will make new partition permanent.

sudo nano /etc/fstab

Add bellow lines.

/dev/sda5             swap            swap            default          0   0

In the last, run this command and activate Swap space.

sudo swapon /dev/sda5

Now reboot the system and all changes are takes effective.

2. Using fallocate command

This is the easy and fast method than the above method.

First create swap file

sudo fallocate -l 1GB /swapfile

If fallocate fails or it not available, you can also use dd:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Now change the file permission by running chmod command

sudo chmod 600 /swapfile

Create Swap space in the system

sudo mkswap /swapfile

Activate new Swap space

sudo swapon /swapfile

Now modify /etc/fstab file and add new partition in the list using Vim or Nano Editor.

sudo nano /etc/fstab

And add the following lines at the end of file.

/swapfile   none swap    sw 0 0

Verify the Swap space

sudo free -h

That's it, it will create 1GB of swap space.

Conclusion

So, this way you can check and add new Swap space. I hope this article helped you. If you have any question, please write in the bellow comment section.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

No Comments