Linux swap partition (detailed explanation)

Linux swap partition (detailed explanation)

linux

1. What is SWAP

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

Functionally, the swap partition is mainly used to swap part of the data in the memory to the swap space when the memory is insufficient, so that the system will not cause OOM or more fatal situations due to insufficient memory. Therefore, when there is pressure on memory usage and memory recycling begins to be triggered, swap space may be used.

2. What does swappiness adjust?

The file /proc/sys/vm/swappiness can be used to adjust swap-related parameters. The default value of this file is 60, and the possible value range is 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 Operations

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

Defines how aggressively the kernel uses swap:

  • The higher the value, the more actively the kernel will use swap;
  • The lower the value, the less motivated to use swap.
  • If this value is 0, the total amount of memory used by free and file-backed pages is less than the high water mark.
  • mark), no exchange will occur. Adjusting it to 0 means trying to reclaim memory by clearing the cache.
  • A value of 100 means that when memory is reclaimed, reclaiming memory from the cache has the same priority as swapping. That is to say, if 100M of memory is currently required, there is a high probability that 50M of memory will be cleared from the cache, and then 50M of anonymous pages will be swapped out, and the recovered memory will be given to the application for use. But this also depends on whether there is space in the cache and whether swap can exchange 50m.

file-backed: This is the size of the file mapping page mentioned above.

3. When will the swap operation be performed?

There are two memory recycling mechanisms: kswapd cycle checking and direct memory recycling. When the requested memory is greater than the remaining memory, direct recycling will be triggered. So what are the conditions that trigger recycling during the kswapd process periodic check? From a design perspective, the kswapd process needs to periodically check the memory and start memory recycling when a certain threshold is reached. This so-called threshold can be understood as the current memory usage pressure. That is, although we still have remaining memory, when the remaining memory is relatively small, that is, when the memory pressure is high, we should start trying to reclaim some memory. This can ensure that the system has enough memory as much as possible for sudden memory requests.

Kswapd decides whether to start reclaiming memory based on the memory water level mark. If the mark reaches low, it starts reclaiming until the remaining memory reaches high.

View the current system memory watermark
$ cat /proc/zoneinfo

4. Priority of swap partition

You can use the -p parameter to specify the priority of the swap space. The larger the value, the higher the priority. The range of numbers that can be specified is from -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

Put an entry in /etc/ fstab to make it take effect on every Linux reboot:

/dev/sdc1 swap swap pri=0 0 0

5. Start and stop swap

$ swapoff -a stop $ swapon -a start

6. Create swap space

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

Enable swap file $ swapon swapfile

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

Turn off swap space $ swapoff swapfile
$ swapon -s
Filename Type Size Used Priority
/swap.img file 2097148 3156 0

The above is the detailed content of Linux swap partition (detailed explanation). For more information about Linux swap partition, please pay attention to other related articles on 123WORDPRESS.COM! , I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Linux index node inode
  • linux No space left on device 500 error caused by inode fullness
  • Linux Network Setup Details
  • How to use MyCat to implement MySQL master-slave read-write separation in Linux
  • Hidden overhead of Unix/Linux forks
  • Learning about UDP in Linux
  • C++ Network Programming under Linux epoll technology and IOCP model under Windows
  • How many ports can a Linux server open at most?
  • Details of Linux file descriptors, file pointers, and inodes

<<:  Div picture marquee seamless connection implementation code

>>:  Solve the matching problem in CSS

Recommend

Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA

1. Add the plug-in and add the following configur...

Detailed explanation of the syntax and process of executing MySQL transactions

Abstract: MySQL provides a variety of storage eng...

js regular expression lookahead and lookbehind and non-capturing grouping

Table of contents Combining lookahead and lookbeh...

Detailed explanation of common usage of MySQL query conditions

This article uses examples to illustrate the comm...

Vue uses the video tag to implement video playback

This article shares the specific code of Vue usin...

HTML table layout example explanation

The elements in an HTML document are arranged one...

CSS3 realizes the effect of triangle continuous enlargement

1. CSS3 triangle continues to zoom in special eff...

How to install docker on ubuntu20.04 LTS

Zero: Uninstall old version Older versions of Doc...

Analysis of the principles and usage of Docker container data volumes

What is a container data volume If the data is in...

How to add, delete and modify columns in MySQL database

This article uses an example to describe how to a...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

JavaScript implements simple scroll window

This article example shares the specific code of ...