An example of operation method of adding swap to CentOS7 system

  • 2021-07-06 12:06:15
  • OfStack

Preface

swap is a special file (or partition) located on disk and belongs to Part 1 of "Virtual Memory". Popular point is the spare tire of memory. Under the condition of sufficient memory, there is basically no swap (related to settings); When the memory is not enough, the system will move part of the data in the memory to swap to free up the memory for the running program.

Using swap can make the system run more or larger programs, but because the reading and writing speed of swap is far lower than that of memory, frequent use of swap may cause delay jamming of the system and programs.

Is a large memory computer swap necessary?

For computers with small memory, swap is very necessary. The existence of swap can make it possible to execute memory-eating programs, and it is better to execute slowly than to run or collapse. However, modern computers often have more than 8G of memory. If they are not running large-scale or professional software, the memory is enough for daily use. In this case, is swap necessary?

The answer depends on the usage scenario. If the desktop Linux is running on the personal computer and the sleep function is turned on, the swap partition not less than the memory should be divided; If it is a server with enough memory and does not shut down all the year round, there is no problem without swap.

Do you want to set swap for ssd hard disk?

Previous ssd hard drives have read-write life problems, so it is not recommended to put swap files or partitions on ssd hard drives.

At present, ssd hard disk technology has been upgraded and smarter. If there is swap demand, it is no problem to put it on ssd hard disk.

How good is the swap setup?

If hibernation is turned on, the swap partition should be divided, and the partition size should not be smaller than the memory. The recommended value is "memory size + 2G".

For servers without hibernation function, swap can be a file. A practical method to determine the size of swap is: 1G memory and below, and swap size is twice of memory; 2-4G memory, the same size as memory; 5-16G memory, swap can be fixed as 4G;; 17-32G memory, 8G swap; 33 G and above, fixed as 16G swap.

The above method is not absolute, and the size of swap can be increased or decreased as appropriate for special needs.

How to increase swap?

swap can be specified as a partition (separate mount point) when installing the operating system, or can be dynamically added and removed after the system is installed.

View swap

The free command can view swap information for system activity, such as:


$ free -m
#  The output is as follows 
#  total used free shared buff/cache available
# Mem:  7976 4979  328  124 2669 2703
# Swap:  0  0  0

Line 2 is the swap information, and you can see that there is no active swap.

You can also view the active swap with the swapon command, for example:


$ swapon -s
#  No swap Information, so there is no output 
#  Have swap The output of is 
# Filename  Type Size Used Priority
# /swap     file 2097148 281052 -2

Add swap

Assuming that we intend to run a program that takes up a lot of memory and 8G has insufficient physical memory, we can increase the virtual memory available to the system with swap. The operation method is as follows:

1. Select or create a file as swap (partition can also be used). Commonly used commands for creating files with specified size are fallocate and dd. For example, create a file of 8G size:


# fallocate
sudo fallocate -l 8G /swap
# dd
sudo dd bs=1GB count=8 if=/dev/zero of=/swap 

Because the replication process of dd is slow to execute, it is recommended to use fallocate in practice;

2. The swap file will store the data in memory, and the access of other users should be restricted for security reasons: sudo chmod 0600 /swap;

3. Format the file as an swap file: sudo mkswap /swap;

4. Enable the swap file: sudo swapon /swap; If you do not want to use swap, use the swapoff command to uninstall: sudo swapoff /swap;

5. If you want the swap file to be loaded automatically after the system starts up, add 1 line to the/etc/fstab file: /swap swap swap sw 0 0 .
After you add swap, you can view swap information using the swapon-s or free-m commands.

swap Related Settings

One setting parameter that is most directly related to swap and affects system performance is vm. swappiness. Its value is an integer from 0 to 100, indicating what percentage of memory is used up to start using swap. 100 means to use swap whenever possible, and 0 means to use swap spare tire only when physical memory is insufficient. For servers, it is recommended to be between 10 and 30.

The commands to set and change swappiness are: sudo sysctl vm.swappiness=10 . This command is only valid for the current system, and the default value will be restored after the system restarts. To set permanently, add or change the corresponding setting row in/etc/sysctl. conf: vm.swappiness = 10 .

Reference

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-7

Summarize


Related articles: