Swap usually performs better on traditional mechanical hard disks. Using swap on SSDs may cause problems, especially after the hardware is aging. Therefore, for DigitalOcean and other users who use SSD-based cloud hosting services, we do not recommend enabling swap. This will even affect other users who share Host with your virtual machine.
For DigitalOcean users, the best way to improve performance is to update the Droplet. Generally speaking, the performance of the upgraded host will be improved, and it is less susceptible to hardware problems.
swapon -s
, if the command does not return a result, it means that the system has not been configured with swap.free -m
to view the overall memory usage of the system. Here you can see the usage status of memory and swap (display unit is MB):free -m
total used free shared buffers cached
Mem:39533153637811107-/+ buffers/cache:1963756
Swap:004095
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 59G 1.5G 55G 3% /
devtmpfs 2.0G 02.0G 0%/dev
tmpfs 2.0G 02.0G 0%/dev/shm
tmpfs 2.0G 8.3M 2.0G 1%/run
tmpfs 2.0G 02.0G 0%/sys/fs/cgroup
fallocate
command, which can create a file with a pre-allocated space of a specified size. Enter the following command to create a 4GB file: sudo fallocate -l 4G /swapfile
ls -lh /swapfile
sudo chmod 600 /swapfile
ls -lh /swapfile
- rw-------1 root root 4.0G Oct 3011:00/swapfile
Then, use the following command to tell the system to use the file for swap: sudo mkswap /swapfile
Now, this swap file can be used as swap space. Enter the following command to start using the swap: sudo swapon /swapfile
We can enter the following command to confirm whether the setting has taken effect:
swapon -s
Filename Type Size Used Priority
/swapfile file 41943000-1
You can see that the returned result already has the swap we just set. Use the free tool to confirm:
free -m
total used free shared buffers cached
Mem:39533153637811107-/+ buffers/cache:1963756
Swap:409504095
At this point, our swap has been set up, and the operating system will use it when needed.
sudo vim /etc/fstab
/swapfile swap swap sw 0 0
Change Swap configuration (optional)
There are several options involving swap that may affect the performance of the system. In most cases these options are optional
The swappiness parameter determines the frequency at which the system exchanges data from memory to swap space. The value is set between 0 and 100, which represents the strength of the system to exchange data from memory to swap space.
The closer the value is to 0, the more the system tends not to perform swap and only perform swap operations when necessary. Since swap is much slower than memory, reducing reliance on swap means higher system performance.
The closer the value is to 100, the more the system tends to swap. The memory usage habits of some applications are more suitable for this situation, which is also related to the purpose of the server.
Enter the following command to view the current swappiness value: cat /proc/sys/vm/swappiness #30
CentOS 7 defaults to a swappiness of 30, which is a moderate value for most desktop systems and local servers. For VPS systems, a value that may be close to 0 is more appropriate.
Use the sysctl command to modify the swappiness. For example, set swappiness to 10: sudo sysctl vm.swappiness=10
This modification will take effect until the next restart. If you want to permanently modify the value, you need to edit the sysctl configuration file:
sudo vim /etc/sysctl.conf
Paste the following at the end of the file:
vm.swappiness =10
After editing, save and exit, then swappiness will be set to this value every time the server restarts.
Another configuration item that can be considered for change is vfs_cache_pressure, which involves the storage of special file system metafile entries. Frequent reading of this type of information is very performance-consuming, so extending its storage time in the cache can improve system performance.
View the current set value of cache pressure through the proc file system: cat /proc/sys/vm/vfs_cache_pressure #100
This value is relatively high, which means that the system removes inode information from the cache faster. A conservative value is 50, use the sysctl command to set:
sudo sysctl vm.vfs_cache_pressure=50
This command is only valid before restarting. To make this setting effective permanently, you need to edit the sysctl configuration file:
sudo vim /etc/sysctl.conf
Add the following at the end of the file:
vm.vfs_cache_pressure =50
Save and exit, the server will automatically set the cache pressure to 50 after each restart.
At this point, our system memory has gained some breathing space. With swap space, some common problems can be effectively avoided.
If you still encounter out of memory (OOM, out of memory) error messages, or your system cannot run the applications you need, then the best way is to optimize your application configuration or upgrade your server.
Welcome to subscribe to "Uncle K Blockchain"-Focus on blockchain technology learning
Blog address: http://www.jouypub.com
Short Book Homepage: https://www.jianshu.com/u/756c9c8ae984
segmentfault homepage: https://segmentfault.com/blog/jouypub
Tencent Cloud Homepage: https://cloud.tencent.com/developer/column/72548
Recommended Posts