linux swap Switch Partition (Explanation)

  • 2021-11-02 03:44:16
  • OfStack

Directory linux 1. What is SWAP
2. What does swappiness regulate? 3. When does swap operate? 4. Priority of swap partition (priority) 5. Start and stop swap 6. Create swap space

linux

1. What is SWAP


$ swapon -s
Filename    Type  Size Used Priority
/swap.img                               file     2097148 0 -2

Functionally speaking, swap partition mainly exchanges some data on memory to swap space when memory is insufficient, so that the system will not cause oom or more fatal situation due to insufficient memory. Therefore, swap space may be used when memory usage pressure begins to trigger memory reclamation.

2. What does swappiness regulate

/proc/sys/vm/swappiness This file can be used to adjust the parameters related to swap. The default value of this file is 60, and the range of values can be 0-100


$  cat /proc/sys/vm/swappiness
60
$ sysctl -q vm.swappiness
vm.swappiness = 60

$ sysctl vm.swappiness=10
$ sysctl -q vm.swappiness
vm.swappiness = 10

Persistent operation


$ vim /etc/sysctl.conf
vm.swappiness=10    # To the last line, you need to restart to take effect 

Define how active the kernel is to use swap:

The higher the value, the more actively the kernel will use swap;; The lower the value, the less motivated you are to use swap. If this value is 0, the total number of pages used by the memory free and file-backed is less than the high water mark (high water mark). Adjusting to 0 means reclaiming memory by clearing the cache as much as possible. Setting to 100 indicates that when memory is reclaimed, the priority of reclaiming memory from cache and exchanging swap is 1. That is, if 100M memory is required at present, there is a high probability that 50M memory will be cleared from cache, and then anonymous pages will be swapped out of 50M, and the recovered memory will be used by applications. But it depends on whether there is space in cache and whether swap can swap 50m.

file-backed: Is the size of the file mapping page mentioned above

3. When will the swap operation take place?

kswapd cycle check and direct memory reclaim two memory reclaim mechanisms. When the requested memory is larger than the remaining memory, direct recycling will be triggered. So what are the conditions under which the kswapd process triggers recycling during periodic inspection? From the design point of view, the kswapd process should check the memory periodically, and start to recycle the memory when it reaches the threshold of 1. This so-called threshold can be understood as the current usage pressure of memory, That is to say, although we still have remaining memory, when the remaining memory is relatively small, that is, when the memory pressure is relatively high, we should start trying to recycle some memory, so as to ensure that the system has enough memory as much as possible for sudden memory applications.

kswapd decides whether to start reclaiming memory according to the memory water mark. If the mark reaches low, it will start reclaiming until the remaining memory reaches high mark.

View the memory water mark of the current system
$ cat /proc/zoneinfo

4. Priority of swap Partition (priority)

You can use the-p parameter to specify the priority of the relevant swap space. The higher the value, the higher the priority. The number range you can specify is-1 to 32767.


$ swapoff /dev/sdc1; swapon -p 0 /dev/sdc1
$ swapon -s
Filename    Type  Size Used Priority
/dev/sdc1                             file     2097148 0 0

$ cat /proc/swaps
Filename    Type  Size Used Priority
/dev/sdc1                             file     2097148 0 0

/etc/ fstab Put 1 entry so that it takes effect every time Linux restarts:


/dev/sdc1 swap swap pri=0 0 0

5. Start and stop swap


$ swapoff -a   Stop 
$ swapon -a   Start 

6. Create an swap space


 Production swap Documents 
dd if=/dev/sda3 of=./swapfile bs=1M count=1G
mkswap ./swapfile

 Enable swap Documents 
$ swapon swapfile

$ swapon -s
Filename    Type  Size Used Priority
/swap.img                               file     2097148 3340 0
/mnt/swapfile            file     6388156 0 -2

 Shut down swap Space 
$ swapoff swapfile
$ swapon -s
Filename    Type  Size Used Priority
/swap.img                               file     2097148 3156 0


Related articles: