This article has been included in the column of this site
Server Optimization Series
0. Preface
Although the cost of going to the cloud has been greatly reduced, individual developers still cannot afford the cost of high-profile servers. However, in the process of running the program, it is inevitable that the memory is not enough, which will trigger the OOM Killer.
How can I avoid out of memory errors in my app? The easiest way is to increase the swap space.
What is swap space?
Swap is a reserved space on the storage disk, where the operating system can temporarily store some things that cannot fit in the memory.
In short, Swap is a piece of virtual memory, which, if set properly, is equivalent to increasing the available memory of the server. Although reading and writing from Swap is slower than memory, it can prevent service interruption when the server memory is exhausted, which is a safety net when memory is insufficient.
Without Swap, once the server runs out of memory, it will start killing applications to free up memory, or even crash, which will cause you to lose some data you haven't had time to save, or cause a crash. Some applications explicitly require the system to configure swap to ensure reliable data access.
Note: swap usually performs better on traditional HDDs, using swap on SSDs can cause problems, especially as the hardware ages.
Therefore, for users using SSD-based cloud hosting services, we do not recommend enabling swap.
1. Check the Swap information of the system
First we need to check the system's storage to see if swap has been configured.
implement:
1 | swapon - s |
In general, results are returned:
1 | 1 |
If the command does not return a result, it means that the system has not been configured with swap.
We can also use the free tool to view the overall memory usage of the system. Here we can see the usage status of memory and swap (displayed in MB):
1 | free - m |
Return result:
1 | total used free shared buffers cached Mem: 3953 315 3637 8 11 107 -/+ buffers/cache: 196 3756 Swap: 0 0 4095 |
2. Check available storage space
implement:
1 | df - h |
return:
1 | Filesystem Size Used Avail Use% Mounted on /dev/vda1 59G 1.5G 55G 3% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 8.3M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup |
You can see from the first row that there is still 59GB of storage space left.
3, choose the appropriate size
What is the appropriate swap space? In general, Swap should be set to 1.5x to 2x the memory capacity.
However, for users with more than 16GB of memory, it is recommended to set it to 4GB.
My physical memory is 4GB, and 4GB of virtual memory has been set on the system disk, but it is still a little stretched, so I want to set 8GB of Swap on the data disk.
4. Create Swap file
implement:
Comment first then view it after your comment is approved. Join QQ Group to display all hidden texts.
5. Automatically mount at boot
After the system restarts, the swap will be invalid. To make the swap take effect automatically after the system restarts, we can modify the fstab file.
edit:
/etc/fstab
Add the following line to the end of the file to tell the operating system to automatically use the swap file you just created:
Comment first then view it after your comment is approved. Join QQ Group to display all hidden texts.
6, optimize configuration
There are several settings involving swap that may affect system performance.
(1) Swappiness
The swappiness parameter determines how often the system swaps data from memory to swap space, and can be set between 0 and 100.
The closer the value is to 0, the more inclined the system is not to perform swap, and only perform swap operations when necessary. Since swap is much slower than memory, less reliance on swap means higher system performance.
The closer the value is to 100, the more likely the system is to perform swaps. The memory usage habits of some applications are more suitable for this situation, which is also related to the purpose of the server.
implement:
1 | cat /proc/sys/vm/swappiness |
Check out swappiness.
CentOS 7 is set to 30 by default.
The swappiness can be modified using the sysctl command. For example, with swappiness set to 10:
1 | sudo sysctl vm.swappiness=10 |
This modification will remain in effect until the next reboot. If you want to change this value permanently, you need to edit the sysctl configuration file:
/etc/sysctl.conf
Paste the following at the end of the file:
1 | vm.swappiness = 10 |
After editing, save and exit, and then the swappiness will be set to this value every time the server restarts.
(2) Cache pressure
This configuration item involves the storage of special filesystem metafile entries. Frequent reading of such information is very performance-intensive, so prolonging its storage time in the cache can improve system performance.
View the current setting of cache pressure through the proc file system:
1 | cat /proc/sys/vm/vfs_cache_pressure |
Use the sysctl command to set:
1 | sudo sysctl vm.vfs_cache_pressure=50 |
To make this setting permanent, edit the sysctl configuration file:
/etc/sysctl.conf
Add the following at the end of the file:
1 | vm.vfs_cache_pressure = 50 |
Save and exit, the server will automatically set the cache pressure to 50 after each restart.
7. Summary
At this point, our system memory has gained some breathing room. Having swap space can effectively avoid some common problems.
If you still get out of memory (OOM, out of memory) error messages, or your system can't run the application you need, then the best way is to optimize your application configuration or upgrade your server, but configure swap Space is also a flexible saving option.
Server optimization (3) Set virtual memory
Comments