Explanation of swap in Linux


In the last article, we introduced the physical storage structure of Linux and how to use itfdiskCommand to partition, using themkfsCommand to format the partition, using themountCommand to mount. But careful friends will find that when we restart the system, we find that the new partition is missing. If you want to make the mount of this device file permanent, you need to write the mount information to the configuration file/etc/fstabMedium:

Explanation of swap in Linux

/dev/sdb1 /myFS xfs defaults 0 0

In the above command, the meaning of each column field is as follows:

field significance
Equipment file Generally, it is the path of the device + the name of the device, and it can also write the unique identification code (UUID)
Mount directory Specify the directory to be mounted to, which needs to be created before mounting
Format type Specify the format of the file system, such as ext3, ext4, XFS, swap, iso9660 (this is a CD device), etc
Permission options If it is set to defaults, the default permissions are: RW, suid, dev, exec, auto, nouser, async
Backup or not If it is 1, dump will be used for disk backup after power on. If it is 0, no backup will be made
Self test or not If it is 1, disk self check will be performed automatically after power on; if it is 0, disk self check will not be performed

As we all know, reading and writing data directly from memory is much faster than reading and writing data from hard disk, so sometimes we prefer to read and write all data in memory. However, it is impossible in practice, because the memory is limited. In order to solve this problem, the concepts of physical memory and virtual memory are introduced.

Basic concepts of swap

Physical memory is the memory size provided by the system hardware, which is the real memory. Compared with physical memory, there is also a concept of virtual memory in Linux. Virtual memory is a strategy to meet the shortage of physical memory. Its design purpose is to solve the problem of shortage of real physical memory. It is a piece of virtual logical memory by using disk space. The disk space used for virtual memory is called swap space.

Explanation of swap in Linux

[mapping diagram of virtual memory and swap space]

As an extension of the physical memory, Linux will use the virtual memory of swap partition when the physical memory is insufficient. More specifically, the kernel will temporarily store the data that is not commonly used in the temporary memory to the hard disk, so as to free up the physical memory space. In this way, the physical memory is released, and the memory can be used for other purposes. When the original content is needed, the information will be read back into the physical memory from the swap space.

But because the swap partition reads and writes data through the hard disk device after all, the speed is certainly slower than the physical memory, so only when the real physical memory is exhausted will the resources of the swap partition be called.

Linux memory management adopts paging access mechanism. In order to make full use of the physical memory, the kernel will automatically exchange the infrequent data blocks in the physical memory to the virtual memory at an appropriate time, and keep the frequently used information in the physical memory.

Of course, to understand the memory mechanism of Linux, we need to master the following points:

  1. Linux system will exchange pages from time to time to keep as much free physical memory as possible
  2. According to the principle of “most frequently used recently”, Linux exchanges some infrequently used page files into virtual memory

3. Pages with swap space will be swapped to physical memory first. If there is not enough physical memory to hold these pages at this time, they will be swapped out immediately. In this way, there may not be enough space in virtual memory to store these swap pages, which will eventually lead to Linux false crash, service exception and other problems

Swap quota size

Therefore, it is very important to reasonably plan and design the use of Linux memory. The size of physical memory and swap space depends on the actual hard disk size. Let’s take a look at RedHat’s official suggestions

Amount of RAM in the system Recommended swap space recommended swap space size Recommended swap space if allowing for hibernation
⩽ 2GB 2 times the amount of RAM 3 times the amount of RAM

2GB – 8GB |Equal to the amount of RAM| 2 times the amount of RAM
8GB – 64GB| At least 4 GB| 1.5 times the amount of RAM
64GB| At least 4 GB| Hibernation not recommended

In our actual production environment, the size of the swap partition is generally 1.5-2 times of the real physical memory.

How to add swap partition

Before adding the swap partition, we can still use the/dev/sdbThe equipment is partitioned. I will allocate 4G space first. The operation process is shown in the figure below:

Explanation of swap in Linux

As you can see from the figure above, the second partition name we separated is/dev/sdb2, using the swap partition specific formatting commandmkswapTo format the new primary partition:

$ mkswap /dev/sdb2

Explanation of swap in Linux

In order to improve the effect of the experiment, let’s take a look at the current swap size

$ free -m

Explanation of swap in Linux

Use the swapon command to formally mount the prepared swap partition device into the system. We can use it againfree -mCommand to view the size change of swap partition (from 2047mb to 6143mb)

$swapon /dev/sdb2

Explanation of swap in Linux

Similarly, in order to enable the new switch partition device to take effect after system restart, we also need to write the following information to the//etc/fstabIn the configuration file:

/dev/sdb2 swap swap defaults 0 0 

Remember to save the file after writing.

Explanation of swap in Linux