How To Add Swap Space On Ubuntu

In this post, I will guide you on How To Add Swap Space On Ubuntu. Use the hard disk as temporary memory to supplement physical memory (RAM) when handling tasks that are larger than the capacity of existing RAM.

To start, you should prepare yourself a VPS running Ubuntu operating system. We choose to use VPS provided by Vultr to bring speed and stability to the website. If you have not used any VPS service, please click here to sign up for an account and get $100 for free.

What is Swap Space?

Swap space is a space on the hard disk which is a substitute of physical memory… Virtual memory is a combination of RAM and disk space that running processes can use. Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full.

Swap is not considered a replacement for physical memory. Since swap space is part of the hard drive, it has a slower access time than physical memory by about 10 to 1000 times. If your system is constantly running out of memory, you should add more RAM.

Steps To Add Swap Space On Ubuntu

Note: Use ​root or user with sudo privileges can activate the swap file.

I will create a 4GB swap file. You can add a swap file with the size you want.

1. Create a swap file.

sudo fallocate -l 4G /swapfile

If faillocate is not installed or an error message saying fallocate failed: Operation not supported then use the following command to create the swap file:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=4194304
Note: bs=1024 means read and write up to 1024 bytes at a time and count=(1024 x 4096)MB size of the file.

2. Set the file permissions to 600 for the swap file.

sudo chmod 600 /swapfile

3. Create a swap area on the file:

sudo mkswap /swapfile

You should see the output like this:

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=9888e511-ff44-4cb3-a999-c9b072a75a67

4. Enable the swap file:

sudo swapon /swapfile

To make the change permanent open the /etc/fstab file:

sudo nano /etc/fstab

Paste the following line:

/swapfile swap swap defaults 0 0
Paste the following line to fstab file

5. Now verify the swap file was created by using the swapon command.

sudo swapon --show

You should see the output like this:

NAME      TYPE SIZE USED PRIO
/swapfile file   4G   0B   -2

Using the free command:

sudo free -h

You should see the output like this:

              total        used        free      shared  buff/cache   available
Mem:          1.9Gi       885Mi       189Mi        61Mi       912Mi       849Mi
Swap:         4.0Gi          0B       4.0Gi

Adjusting The Swappiness Value

You can check the current swappiness value by typing the following command:

cat /proc/sys/vm/swappiness
Output:
60

To set how often the kernel can use the swap file, open the file /etc/sysctl.conf.

sudo nano /etc/sysctl.conf

And add the line below:

vm.swappiness=10
Set kernel for the swap file
Note: The default value of how frequent swap space can be used is 60 (maximum value is 100). The higher the number, the more frequent swap space utilization by the kernel.

Removing a Swap File

1. Deactivate the swap space:

sudo swapoff -v /swapfile

2. Remove /swapfile swap swap defaults 0 0 line has been entered from the /etc/fstab file.

3. Remove swapfile file.

sudo rm /swapfile

This is the end of the How To Add Swap Space On Ubuntu.

Follow us for the more helpful posts!

We hope this is a useful post for you.

You can read more useful posts like How To Display Magento 2 Module List.

Thank you for reading!

4.5 2 votes
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x