How to add swap space to your Ubuntu

  • avatar
  • 1.7K Views
  • 5 mins read

Swap space is the area on a hard disk. It holds memory pages that are temporarily inactive. Swap space is used when your operating system decides that it needs physical memory for active processes and the amount of unused physical memory is insufficient. When this happens, inactive pages from the physical memory are then moved into the swap space, freeing up that physical memory for other uses.

How much swap do I need?

For modern systems, your swap space should be at a minimum be equal to your physical memory (RAM) size if you use hibernation, otherwise you need a minimum of round(sqrt(RAM)) and a maximum of twice the amount of RAM depending upon the amount of hard disk space available for the system.

How do I add swap partition?

Dedicated Swap partition is the recommended option. However, it can be risky to assign or change partitions when you already have an operating system installed. In this case, we recommend to use the Swap file instead.

If you are installing a fresh operating system you can follow installation guide steps to setup Swap partition.

How do I add swap file?

We will allocate a file of a 4GB file called swapfile  in our root (/) directory:

sudo fallocate -l 4g /swapfile

If fallocate fails or it's not available, you can use dd (4G = 4194304B):

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

We need to set the swap file permissions to 600 to prevent other users from being able to read potentially sensitive information from the swap file:

sudo chmod 600 /swapfile

Format the file as swap and enable it:

sudo mkswap /swapfile && sudo swapon /swapfile

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

sudo nano /etc/fstab

and paste the following line:

/swapfile swap swap defaults 0 0

swap_fstab.png

Finally, verify that the swap is active:

sudo swapon --show

swap_show.png

Adjusting the swappiness

Swappiness is a Linux kernel property that defines how often the system will use the swap space. It can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.

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

cat /proc/sys/vm/swappiness

swap_swapiness.png

While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.

To make this parameter persistent across reboots, open /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

and append:

vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

Disable and remove a Swap file

Disable the swap file from the running system:

sudo swapoff /swapfile

Next, remove the swap file entry from the /etc/fstab file:

/swapfile swap swap defaults 0 0

Finally, remove the actual swapfile file using the rm command:

sudo rm /swapfile

Empty Swap

You can use the following command to disable swap and get it manually back into RAM:

sudo swapoff -a

And then, enable it again:

sudo swapon -a

Conclusion

Don't forget that the access time for swap is slower, depending on the speed of the hard drive. Do not consider it to be a complete replacement for the physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap file(s).

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.