Preface
Because I was too shy to talk about it, I only had 2G of memory. When the project was running, the memory occupied was reported, so sometimes downtime would occur. Later I found that I could increase the virtual memory space by using Memory Capacity. Let’s get to the point and explain the creation and deletion of swap partitions under CentOS7.
Creation of swap partition
1. Check disk usage
free -h
And Mem means that the memory when purchasing Alibaba Cloud, we can see that it is only 1.7G (while talking, tears flowed down)
Here you can see that swap is 0
2. Add Swap partition
Use the dd command to create a swap file named swapfile (the file name and directory are arbitrary):
dd if=/dev/zero of=/var/swapfile bs=1024 count=4194304
dev/zero is a special character device (input device) in Linux. It can be used to create an empty file with a specified length for initialization, such as a temporary swap file. This device provides 0 endlessly and can provide any number you need. .
bs=1024: The block byte size read/outputted by the unit data block (block) at the same time is 1024 bytes, which is 1KB, bs (that is, block size).
count = 4194304 represents 4G
The specific calculation formula is: 1KB * 4194304 =1KB *1024(k)*1024*4 = 4194304 =4G
If you need to adjust the size of the swap area, you can set other settings by yourself.
After executing the command, 4G read and write operations will be performed, so there will be some lags. Please wait patiently.
After execution is completed
3. Format the swap file and convert it to a swap partition
mkswap /var/swapfile
Execution completed display
4. Mount and activate the partition
swapon /var/swapfile
Execution completed display
When executing the above command, a similar prompt may appear: "Unsafe permission 0644, it is recommended to use 0600". Don't be nervous. It has actually been activated. You can ignore the prompt or follow the system's suggestions to modify the permissions: ( You can also continue regardless of it. go down )
chmod -R 0600 /var/swapfile
5. Check whether the new swap partition is added normally and activated for use.
free -h
6. Modify the fstab configuration and set the partition to be automatically mounted at boot.
echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab
7. Check whether swap memory has been used
top
We can see that the swap space has been successfully used
Change Swap configuration
Generally, by default, when we turn on the swap virtual memory space, the default seems to be that when the memory usage is 50%, the swap space will start to be used. This will cause a situation where the physical memory itself has not been fully used. To use virtual memory, this will definitely affect our usage efficiency, so how can we avoid this situation?
The answer is: it can be managed through the swappiness value. swappiness indicates the system's dependence on the Swap partition, ranging from 0 to 100. The larger the value, the higher the degree of dependence. That is, the higher the value, the more likely the Swap partition will be used.
Therefore, we do not want our machines to rely too much on the Swap partition. We will only use the swap space when our load exceeds a certain percentage, so this also determines that our value is not very large, generally set to 10 ~50 about.
Of course, if your friend has an SSD, then this value can be slightly larger.
Next we check the current swappiness value:
cat /proc/sys/vm/swappiness
Modify the swappiness value, taking 10 as an example:
sysctl vm.swappiness=10
The settings are permanently valid and will take effect after restarting the system.
echo "vm.swappiness = 10" >> /etc/sysctl.conf
Finally, we can see through the top command again that the swap space has been used.
Delete the swap partition ( do not do this if you want to increase virtual memory now )
The deletion of the swap partition is only used when deleting the partition in the future. If you are increasing virtual memory now, you can ignore this step.
1. Stop using the swap partition
swapoff /var/swapfile
2. Delete the swap partition file
rm -rf /var/swapfile
3. Delete or comment out the boot auto-mount configuration content we previously added in the fstab file.
vim /etc/fstab
#把下面内容删除
/var/swapfile swap swap defaults 0 0