×
Community Blog How to Add Swap Space on Ubuntu 20.04

How to Add Swap Space on Ubuntu 20.04

This article explains how to add Swap Space on Ubuntu 20.04.

By Alain Francois

When running your operating systems, there are some criteria that you need to take into account, such as the CPU, disk size, and memory. The most important one is memory. There are two basic types of memory in a computer on Linux systems, random access memory (RAM) and the Linux swap space size.

The RAM helps store data and programs while they are being actively used by the computer, and the swap space is used when the amount of physical RAM is full.

what is swap space linux?

Linux swap space was a dedicated swap partition used when a system needed more memory resources and when the RAM was full. It is a portion of hard drive storage that has been set aside to temporarily store data that the system can no longer hold in RAM. It should not be considered as a replacement to the physical memory. The information written to disk will be significantly slower than information kept in RAM. For example, the RAM content is stored in the swap space in hibernation mode, so power can be cut off completely. That is why it's recommended to have a swap size as large as the RAM.

How to Add Swap Space Linux?

Linux swap spaces were created during system installation.most of the time,However, it has been given the possibility to add or linux create swap spaces even after system installation.

How Much Swap Space Ubuntu?

How much should be the linux swap space size? Should the swap be double of the RAM size or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM?

Perhaps these are the most common asked questions about choosing swap size while installing Linux.It’s nothing new. There has always been a lot of confusion around swap size.For a long time, the recommended swap size was double of the RAM size but that golden rule is not applicable to modern computers anymore. We have systems with RAM sizes up to 128 GB, many old computers don’t even have this much of hard disk.But what swap size would you allot to a system with 32 GB of RAM? 64GB? That would be a ridiculous waste of hard disk, won’t it?

Before we see how much swap size you should have, let’s first quickly know a thing or two about swap memory. This will help you understand why swap space is used.

Nowadays, swap space can be a dedicated swap partition or a swap file. It means you can still create it even after the installation of your Linux system.

Prerequisite

As our article is related to Ubuntu 20.04, you will need to run an instance. We will use an Alibaba Cloud Elastic Compute Service (ECS) instance. It is a virtual server that includes basic components, such as CPU, memory, operating system (OS), network configurations, and Alibaba Cloud disks. There are many Alibaba Cloud ECS instances families, and the families are also classified by category to help users choose an instance that fits their needs (CPU optimization, memory, etc.) In our case, we will order an ECS instance with 2GB of memory.

1

Then, we will choose the OS:

2

Check an Active Swap

After ordering and running your ECS instance, check and see if the system already has some swap space configured or not.

$ free -m

3

If you have no result on the swap line, it means you have no active swap space.

Creating a Swap File

We will create a file that will be used as a swap space. The file will be 2GB (2 * 1024 = 2048). There are two ways to create a swap file with the fallocate and the dd commands.

The fallocate command creates a file with the specified size instantly. You can install the util-linux package:

$ sudo apt install util-linux 

You create the swap file by indicating the size:

$ sudo fallocate -l 2G /swapfile

You can verify the operation:

$ ls -lh /swapfile
-rw-r--r-- 1 root root 2.0G Dec  1 03:23 /swapfile

If you can't use the fallocate command for whatever reason, you can use the dd command:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

bs is the block size, and bs * count = bytes will be allocated (here 2 GB). The bs = 1M stands for mega as we are assigning 1MB block size, and we are allocating 2048 * 1MB (=2GB) to swap.

Set the Permissions

We need to turn the swap file into swap space, and we should only allow users with root privileges to read the contents.

$  sudo chmod 600 /swapfile

Set Swap Space

After giving the right permissions, we can format it to swap:

$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=ef768164-9056-4e78-8e69-759004f31844

Enable the Swap File

Now, we can enable the swap file so our system can start to use it:

$ sudo swapon /swapfile   

We can check the result

$ free -h

4

The swap is detected now.

Mount the Swap File Automatically

If you reboot the server, the current changes will be gone since the configurations that have been done thus far are only available for the current session. We need to edit the /etc/fstab file and add it at the end of the new line file to make it permanent. Make sure to create a backup of the file.

$ sudo vim /etc/fstab

5

Remove a Swap File

You need to deactivate the swap space first To deactivate and delete the swap file:

$ sudo swapoff -v /swapfile
swapoff /swapfile

Then, you need to remove the swap file line /swapfile none swap sw 0 0 that we added to the /etc/fstab file.

After that, we can delete the swap file:

$ sudo rm /swapfile

You can verify it after that:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          888Mi       265Mi       292Mi       1.0Mi       329Mi       475Mi
Swap:         0B          0B          0B

Configure the Swappiness Parameter

The swappiness parameter is a kernel parameter that defines how much (and how often) the Linux kernel will copy RAM contents to a swap. Swapiness is a value between 0 and 100. A swappiness value close to 0 means the disk will be avoided unless necessary (if you run out of memory), and a swappiness close to 100 means programs will be swapped to the disk almost instantly.

On Ubuntu, the default swappiness value is set to 60.

$ cat /proc/sys/vm/swappiness
60

You can change the value and make it persistent by adding a line at the end of the configuration file /etc/sysctl.conf:

$ sudo vim /etc/sysctl.conf
vm.swappiness=20

Then, reload the sysctl configuration file:

$ sudo sysctl -p
vm.swappiness = 20

Why Should I Use Swap Space?

A swap partition is highly recommended if you have a computer with a small amount of memory. Also, if there are too many applications running that need a lot of RAM, your system can get into trouble and applications will crash. Therefore, swap gives admins enough time to react to low memory issues.

Conclusion

The swap gets used once the physical memory is used up. The swap disk is much slower than RAM, meaning performances can go down, and thrashing can occur. However, as long as you have enough RAM, having a swap area of any size won’t hurt performance at all.

Releted Article

[Red Flag asianux Server 3 System Management: Swap Space](https://topic.alibabacloud.com/article/red-flag-asianux-server-3-system-management-swap-space_3_12_516982.html
)
Swap space is a part of the system that is partitioned from the hard disk, and when physical memory (RAM) is filled, inactive pages in memory are moved to swap space.

Configuring Agents in Swap space on Windows Azure on Linux VMS
This article provides a series of simple steps for configuring file-based swap space on a resource disk (/mnt/resource). It should be noted, however, that the steps described at the time apply to the configured and running VM. Ideally, people want to automatically configure swap space when the VM is configured, instead of waiting for a bunch of commands to be run manually later.

0 0 0
Share on

Alibaba Cloud Community

1,042 posts | 256 followers

You may also like

Comments