Set virtual memory in Linux-create and enable Swap zone and linuxswap
If your server always reports insufficient memory and the service is forced to kill due to insufficient memory, enabling the swap area is a good choice for virtual memory. If it is an SSD hard disk, the normal read/write speed is above 300 MB/S. After swap is enabled, the performance has improved a lot, especially in processing scripts that consume a large amount of memory.
Principle for creating a swap zone:
1. The size of the created swap zone should be greater than the actual physical memory capacity, but not too large, to avoid wasting hard disk space.
2. If memory I/O requests are frequent, and the wait time of the I/O queue in a single swap zone is too long, you can create several more swap zones.
3. In principle, priority should be given to the device with the fastest I/O speed.
Procedure:
1. Create blank files for hard disk storage in the swap area.
12 |
# Create a 1G blank SWAP swap zone file dd if =/dev/zero of =/SWAP bs = 1 M count = 1024 |
Generally, 2 ~ physical memory is created ~ 2.5 times the size of the file as the swap area.
2. format the file as a swap file system using mkswap
12 |
Mkswap-f/swap #-f uses the file as the swap Zone |
3. Enable the created Swap file
4. If necessary, you can enable the swap file exchange zone automatically upon startup, modify/etc/fstab, and add a line
1 |
/Swap defaults 0 0 # enable swap upon startup |
5. If you do not need to enable swap or adjust the swap size, you can use the swapoff command to disable swap.
After swap is disabled, delete the corresponding swap file to delete the swap area. To adjust the size of the swap area, create a new one from the first part.