If your server is always reporting low memory, and often because the service is forced to kill because of insufficient memory, without adding physical memory, enabling the swap Swap area as virtual memory is a good choice, I buy the Digitalocean VPS 512M memory is basically not enough , fortunately, the VPS used by the hen SSD, normal read and write speed above 300mb/s, enable swap after the performance improved a lot, especially in the processing of memory-consuming scripts
Create a swap area principle:
1. The swap size created should be larger than the actual physical memory size, but not too large to avoid wasting hard disk space.
2. If the memory IO request is frequent, and the single swap swap area IO queue is too long, you can create several swap swap areas.
3. In principle, priority is created on the fastest-io device.
To create a step:
1. Create a blank file for the Swap swap area hard drive storage.
The code is as follows |
Copy Code |
#创建一个1个G的SWAP交换区空白文件 DD If=/dev/zero Of=/swap bs=1m count=1024 |
You typically create a 2~2.5 of physical memory as a swap area.
2. Use mkswap format file as swap file system
The code is as follows |
Copy Code |
Mkswap-f/swap #-f uses files as swap swap area |
3. Enable the swap file you just created
The code is as follows |
Copy Code |
Swapon/swap |
4. If necessary, you can set the power-on automatically enable swap file swap area, modify/etc/fstab, add one line
The code is as follows |
Copy Code |
/swap Swap defaults 0 0 #启动即启用swap |
5. If you do not need to enable swap or need to adjust the swap size, you can use the Swapoff command to turn off swap.
The code is as follows |
Copy Code |
Swapoff/swap |
If you close swap and delete the corresponding swap file, you can remove the swap swap area, and if you need to adjust the swap size, recreate it from the first part.