Alibaba Cloud Server creates a swap partition to solve the problem of insufficient memory

Under normal circumstances, the newly purchased Linux system of Alibaba Cloud does not have a swap partition. When the memory is small, you can use the swap partition to share the pressure of the physical memory.

1. Check whether the swap partition has been enabled in the current system

cat /proc/swaps   
top
free -m

2. If the swap partition function is not enabled, create a special file for the swap partition

dd if=/dev/zero of=/swap_file bs=1M count=2048

Note: The size of this file is the size of count multiplied by the size of bs, and the size of the above command is 2GB

3. Use the mkswap command to make the newly created file into a swap partition

mkswap /swap_file

4. Enable the swap partition and use the command to view the memory usage:

swapon /swap_file
free -m

5. Set up automatic mount at startup

Edit vi /etc/fstab

Add to:

/swap_file swap swap defaults 0 0

or directly enter:

echo "/data/swap swap swap defaults    0  0" >> /etc/fstab

6. Check whether the value in the kernel parameter vm.swappiness is 0. If it is 0, adjust it to 30 or 60 according to actual needs.

cat /proc/sys/vm/swappiness   
sysctl -a | grep swappiness    
sysctl -w vm.swappiness=50

It needs to be briefly explained here that in the Linux system, you can check the value of the content of /proc/sys/vm/swappiness to determine the system's use of the SWAP partition. When the value of the swappiness content is 0, it means that the physical memory is used to the maximum extent, and the SWAP partition will be used only after the physical memory is used up. When the value of the swappiness content is 100, it means that the SWAP partition is actively used, and the data in the memory is replaced to the SWAP partition in time. Note: If you want to modify it permanently, edit the /etc/sysctl.conf file.
We set 50 here, which means that the swap partition will be used when the physical memory is less than 50%.

7. Close the swap partition

swapoff /data/swap   
swapoff -a >/dev/null

8. Regarding the priority of using multiple swap partitions

If you have more than one swap file or swap partition, you can assign each of them a priority value (0 to 32767). The system will use the higher-priority swap area before using the lower-priority swap area. For example, if you have a faster disk (/dev/sda) and a slower disk (/dev/sdb), assign the faster device a higher priority. The priority can be specified in fstab with the pri parameter:    

/dev/sda1 none swap defaults,pri=100 0 0    
/dev/sdb2 none swap defaults,pri=10  0 0

Or via swapon's ?p (or ??priority) parameter:    

swapon -p 100 /dev/sda1

If two or more regions have the same priority, and they are the highest available priority, the pages are allocated among them in a round-robin fashion.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447859&siteId=291194637