My friend has a VPS with CentOS7 on Alibaba Cloud, with 2GB of memory, which is used for testing when developing my own projects;
He runs 5 docker instances on it, runs java programs; there is also a mysql service;
The above 5 docker instances and mysql service occupies a total of 1.35GB of VPS memory, and the system runs very slowly. ssh connection input commands are stuck;
Because the memory is too small, there is no room for optimization. So prepare to enable swap swap space.
Google received a blog post and added swap space through the method provided in the article; but encountered a problem. The
/dev/zero file does not exist in the centos7 system, so I found another blog post.
1. The solution for deleting the
**/dev/zero** file by mistake is to repair and rebuild the file with the following two commands:
[root@localhost sslinux]# mknod /dev/zero c 1 5 [root@localhost sslinux]# chmod 666 /dev/zero
After the repair is completed, use the command to test whether it is available, and it is found that the disk test can be used normally
[root@localhost sq808sq]# dd if=/dev/zero of=test bs=64k count=4k oflag=dsync
At this point, the repair and reconstruction of the
/dev/zero file has been completed (the file is a system special file that cannot be directly copied from the system disk or other systems after it is lost. It can only be repaired by reconstruction, as is the
2. Use the dd command to add swap space (
dd if=/dev/zero of=/var/swap bs=1024 count=4096000 ls -lh /var/swap mkswap /var/swap mkswap -f /var/swap swapon /var/swap free -lh swapon /var/swap more /proc/swaps vim /etc/fstab free -lh
Set swap to automatically mount at boot:
[root@izbp12z8w3jbbgmy9yiz92z ~]# tail -1 /etc/fstab /var/swap swap swap defaults 0 0
However, using the
free -lh command to check the usage of memory and swap at this time, there is still no change.
1.swap is to store "memory that does not need to be used temporarily" on the hard disk, and then load it into the memory when it is used
This situation can only show that there is no "long-term unused memory"
2. The usage of Swap is not directly related to OOM. OOM looks at the amount of applications, not the amount of usage. OOM will be triggered even if your memory is not used up
In fact, the swap space is not used until all the physical memory is consumed. When to use it is controlled by the swappiness parameter value.
[root@rhce ~]# cat /proc/sys/vm/swappiness 60
The default value of this value is
swappiness=0, it means to maximize the use of physical memory, and then the swap space.
swappiness=100, it means that the swap partition is actively used, and the data in the memory is transferred to the swap space in time.
That is, when the memory is large enough, the
swappiness should be set as small as possible; when the memory is small, the
swappiness should be set as large as possible;
[root@rhce ~]# sysctl vm.swappiness=10 vm.swappiness = 10 [root@rhce ~]# cat /proc/sys/vm/swappiness 10
Add the following parameters in the
vm.swappiness=10 [root@izbp12z8w3jbbgmy9yiz92z ~]# sed -i '$a vm.swappiness=100' /etc/sysctl.conf [root@izbp12z8w3jbbgmy9yiz92z ~]# sysctl vm.swappiness vm.swappiness = 100
At this point, the system will use swap as much as possible. You can execute a command that takes up a lot of memory to see the effect;
Check the usage of memory and swap at this time:
[root@izbp12z8w3jbbgmy9yiz92z ~]# free -lh total used free shared buff/cache available Mem: 1.8G 899M 690M 1.5M 249M 779M Low: 1.8G 1.1G 690M High: 0B 0B 0B Swap: 3.9G 715M 3.2G
OK, the problem is solved.
Because I think I may encounter this situation again in the future, so simply record it;