Foreword
Swap is a special file (or partition) located on the disk and is a part of "virtual memory". The popular point is the spare tire of the memory. When the memory is sufficient, there is basically nothing to swap (related to the setting); when the memory is not enough, the system will move part of the data in the memory to swap to free up memory for the running program .
Using swap allows the system to run more or larger programs, but since the read and write speed of swap is much lower than that of memory, frequent use of swap may cause delays in the system and programs.
**Is a large memory computer swap necessary? **
For computers with small memory, swap is very necessary. The existence of swap makes it possible to execute memory-eating programs, and slow execution is better than not running or crashing. However, modern computers often have more than 8G of memory. If they are not running large-scale or professional software, the memory is sufficient for daily use. In this case, is swap necessary?
The answer still depends on the usage scenario. If the desktop version of Linux is running on a personal computer and the hibernation function is turned on, it should be divided into a swap partition that is not less than the memory; if it is a server that has enough memory and does not shut down all the year round, there is no problem without swap.
**Do I need to set swap for ssd hard disk? **
The previous ssd hard disk has the problem of read and write life, so it is not recommended to put the swap file or partition on the ssd hard disk.
The current ssd hard disk technology has been upgraded and smarter. If there is a swap demand, there is no problem with putting it on the ssd hard disk.
**How big is the swap setting? **
If the hibernation function is enabled, swap partitions should be divided, and the partition size cannot be smaller than the memory. The recommended value is "memory size + 2G".
For servers without hibernation function, swap can be a file. A practical way to determine the swap size is: 1G memory and below, the swap size is twice the memory; 2-4G memory, the same size as the memory; 5-16G memory, swap Can be fixed to 4G; 17-32G memory, 8G swap; 33G and above, fixed to 16G swap.
The above method is not absolute, and the swap size can be increased or decreased as appropriate for special needs.
**How to increase swap? **
When installing the operating system, you can specify swap in the form of a partition (separate mount point), or it can be dynamically added and deleted after the system is installed.
View swap
The free command can view the swap information of system activities, for example:
$ free -m
# The output is as follows
# total used free shared buff/cache available
# Mem:7976497932812426692703
# Swap:000
The second line is the swap information, you can see that there is no active swap.
You can also use the swapon command to view the active swap, for example:
$ swapon -s
# There is no swap information, so no output
# The output with swap is
# Filename Type Size Used Priority
# /swap file 2097148281052-2
Increase swap
Suppose we intend to run a program that takes a lot of memory, and 8G of physical memory is not enough, we can use swap to increase the virtual memory available to the system. The operation method is as follows:
1、 Select or create a file as a swap (partition can also be used). Commonly used commands to create a file of a specified size are fallocate and dd. For example, to create an 8G file:
# fallocate
sudo fallocate -l 8G /swap
# dd
sudo dd bs=1GB count=8if=/dev/zero of=/swap
Due to the slow execution of the dd copy process, fallocate is recommended in practice;
2、 The swap file will store the data in the memory. For security reasons, other users' access should be restricted: sudo chmod 0600 /swap;
3、 Format the file as a swap file: sudo mkswap /swap;
4、 Enable the swap file: sudo swapon /swap;
If you don't want to use swap, you can uninstall it with the swapoff command: sudo swapoff /swap;
5、 If you want the swap file to be automatically loaded after the system starts, add a line in the /etc/fstab file: /swap swap swap sw 0 0
.
After swap is added, swapon -s or free -m can be used to view swap information.
swap related settings
The most directly related to swap and a setting parameter that affects system performance is vm.swappiness. Its value is an integer from 0-100, indicating the percentage of memory used to start swap. 100 means to use swap as much as possible, and 0 means to use swap spare tires only when the physical memory is insufficient. For servers, between 10-30 is recommended.
The command to set and change swappiness is: sudo sysctl vm.swappiness=10
. This command is only valid for the current system, and the default value is restored after the system restarts. If you want the setting to take effect permanently, add or change the corresponding setting line in /etc/sysctl.conf: vm.swappiness = 10
.
reference
https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-7
to sum up
The above is the entire content of this article. I hope that the content of this article has a certain reference value for your study or work. Thank you for your support to ZaLou.Cn.
Recommended Posts