DigitalOcean has been used for a long time. a process was killed when PHP was compiled a few days ago. After I spoke with others, I found that the memory was exhausted. In fact, it was because the MySQL process at that time consumed a lot of memory, and later felt it was necessary to manually add Swap (Swap partition), so as to avoid the process being K when compiling or something later. About Swap in Linux
DigitalOcean has been used for a long time. a process was killed when PHP was compiled a few days ago. After I spoke with others, I found that the memory was exhausted. In fact, it was because the MySQL process at that time consumed a lot of memory, and later felt it was necessary to manually add Swap (Swap partition), so as to avoid the process being K when compiling or something later.
In Linux, Swap (Swap partition) is similar to Windows virtual memory, that is, when the memory is insufficient, part of the hard disk space is virtualized into memory, so as to solve the problem of insufficient memory capacity.
So how to manually add Swap in CentOS?
1. check the Swap space
Before setting the Swap file, check whether the existing Swap file exists in the system.
Run the following command:
swapon -s
If the returned information is null, the Swap file does not exist.
2. check the file system
Before setting the Swap file, it is also necessary to check the file system to see if there is enough hard disk space to set the Swap. Run the following command:
df -hal
Check the returned information and make sure there is sufficient disk space.
3. create and allow Swap files
Run the dd command to create a Swap file.
dd if=/dev/zero of=/swapfile bs=1024 count=512k
Parameter explanation:
If = File name: Enter the file name. The default value is standard input. Specifies the source file. <If = input file>
Of = File name: name of the output file. the default value is standard output. Specifies the target file. <Of = output file>
Bs = bytes: set the size of the read/output block to bytes at the same time.
Count = blocks: copy only blocks. the block size is equal to the number of bytes specified by bs.
4. Format and activate the Swap file
The above Swap file has been created and needs to be formatted before it can be used. Run the following command:
mkswap /swapfile
Activate Swap and run the following command:
swapon /swapfile
After completing the preceding steps, run the following command again:
swapon -s
You will find the returned information summary:
Filename Type Size Used Priority/swapfile file 524284 0 -1
If you want to automatically mount Swap when the machine is restarted, you need to modify the fstab configuration.
Use vim to open the/etc/fstab file and add the following line at the end:
/swapfile swap swap defaults 0 0
Finally, grant appropriate permissions to the Swap file:
chown root:root /swapfile chmod 0600 /swapfile
At the same time, we can modify the swappiness of Linux Swap space to reduce the hard disk cache.
Linux uses a part of the hard disk as a Swap partition for process scheduling-processes are running programs-changes unused processes to 'wait (standby )', even 'sleep ', once used, and then changed to 'active (active)', the sleeping process will be in the Swap partition, leave the memory blank to the 'active' process.
If the memory is large enough, it should be told that Linux does not need to use too many Swap partitions. you can modify the swappiness parameter to set it. When swappiness is set to 0, the physical memory is used to the maximum extent, and then the Swap space is used. when swappiness is set to 100, the Swap partition is actively used, in addition, the data in the memory is promptly moved to the Swap space.
In CentOS, the default value of swappiness is 60.
You can see through the following command:
cat /proc/sys/vm/swappiness
Return value 60
We can adjust the value of swappiness to a suitable parameter to optimize the use of Swap. Here we set it to 10.
Run the sysctl command:
sysctl vm.swappiness=10
However, this is only temporary modification. after you restart the system, the default value of 60 will be restored. to set it permanently, you also need to modify sysctl. conf in vim:
vi /etc/sysctl.conf
Add the following line at the end of this document:
# Search for the vm.swappiness setting. Uncomment and change it as necessary.vm.swappiness=10
Input: x to save and exit vim.
This will take effect after the Swap partition is restarted.