Management of swap partitions in Linux

Source: Internet
Author: User

One: Swap zoning concept principle


Swap partition: (Storage of memory overflow data)

Functionally, the swap partition is mainly when the memory is not enough, the part of the memory of the data exchange to swap space, so that the system will not be due to insufficient memory to cause oom or more deadly situation.

First: The basic concept of swap

Premise: Knowing the concept of memory recycling

Why do I need to recycle my memory?

1: The kernel needs to provide enough memory for any burst of memory requests at any time. Therefore, it is generally necessary to ensure that there is enough free space for the kernel. Therefore, it is necessary for the kernel to design a mechanism to recycle memory periodically, so that the use of the cache and other related memory can not keep the system's remaining memory in a very small state for a long time.

2: Forced memory reclamation is triggered when there is a serious request that is larger than the free memory.

Therefore, the kernel in response to these two types of recycling requirements, respectively, implemented two different mechanisms:

    • 1: One is to use the KSWAPD process to check the memory periodically to ensure that the remaining memory is as adequate as possible in normal state. One is the kernel process KSWAPD directly call memory recycling logic for memory recovery;

    • 2: The other is Direct memory Reclamation (DIRECTPAGERECLAIM) , which triggers direct memory reclamation when there is no free memory to meet the requirements when allocating memory. The other is the memory request logic that enters slow path when it is requested to recycle.

The entire scanning process is divided into several loops:

  1. First, scan the Cgroup group on each zone;

  2. Then the page list is scanned with Cgroup memory as the unit;

  3. The kernel will first scan the active linked list of anon, will not frequently put into the inactive linked list, and then scan the inactive linked list, will be active back to active;

    650) this.width=650; "src=" Http://img2.tuicool.com/3amIRjj.jpg%21web "class=" AlignCenter "style=" Height:auto; vertical-align:middle;border:0px none;margin:0px auto 10px;text-align:center; "/>

  4. When the swap is carried out, the inactive page is first swapped out;

  5. If it is a File Mapping page page, determine whether it is dirty data, if it is dirty data is written back, not dirty data can be released directly.

In this sense, the behavior of memory reclamation recycles the use of two types of memory:

    • One is the anon of anonymous page memory, the main means of recovery is swap;

    • The other is the File-backed file mapping page, the main release means is writeback and emptying.

Because of the memory for filebased, there is no need to exchange, the data is originally on the hard disk, the recovery of this part of memory as long as there is dirty data to write back, and empty the memory can be, and later need to read from the corresponding file back.

Summary: There are two ways to reclaim memory (anonymous page Exchange (Swap) and file cache emptying (direct release))

Remark: vim/proc/sys/vm/swappiness

The numbers inside indicate the priority used by swap and cache emptying compared


II: The management of swap partitions
Swapon-s # #查看系统中的swap分区

1: When using a hard disk partition as swap swap space:

FDISK/DEV/VDB # #新建一个swap分区

[[email protected] ~]# fdisk /dev/vdbwelcome to fdisk  (util-linux  2.23.2). changes will remain in memory only, until you decide to  Write them. Be careful before using the write command. command  (m for help):  npartition type:   p   primary   (0 primary, 0 extended, 4 free)    e    extendedselect  (default p): 1invalid partition type  ' 1 ' Command  (m for &NBSP;HELP): q[[email protected] ~]# fdisk /dev/vdbwelcome to fdisk  ( util-linux 2.23.2). changes will remain in memory only, until you decide to  Write them. Be careful before using the write command. command  (m for help):  command  (m for help): npartition type:   p   primary  (0  primary, 0 extended, 4 free)    e   extendedselect   (default p):  [[email protected] ~]# [[email protected] ~]# fdisk  /dev/vdbWelcome to fdisk  (util-linux 2.23.2). changes will remain in memory only, until you decide to  Write them. Be careful before using the write command. command  (m for help):  npartition type:   p   primary   (0 primary, 0 extended, 4 free)    e    extendedselect  (default p): using default response ppartition number  ( 1-4, default 1): first sector  (2048-20971519, default 2048): &NBsp using default value 2048last sector, +sectors or +size{k,m,g}  ( 2048-20971519, default 20971519):  +2gpartition 1 of type linux and  of size 2 GiB is setCommand  (M&NBSP;FOR&NBSP;HELP): tselected  partition 1hex code  (type l to list all codes): l 0   Empty           24  NEC DOS          81  Minix / old Lin bf   Solaris         1  FAT12            27  hidden ntfs win 82  linux  swap / So c1  DRDOS/sec  (fat- 2  xenix root       39  plan 9          83  linux            c4  DRDOS/sec  (fat- 3   xenix usr       3c  partitionmagic  84   os/2 hidden c:  c6  drdos/sec  (fat- 4  fat16 <32m       40  Venix 80286     85   linux extended  c7  syrinx           5  Extended        41  PPC PReP  boot   86  ntfs volume set da  non-fs data      6  FAT16           42   sfs  &nbsP;          87  ntfs volume set db   cp/m / ctos / . 7  hpfs/ntfs/exfat 4d  qnx4.x           88  Linux plaintext de   Dell Utility    8  AIX              4e  qnx4.x 2nd part 8e  linux lvm        df  BootIt           9  aix bootable    4f  qnx4.x 3rd part  93  Amoeba          e1  DOS  Access      a  os/2 boot manag 50  ontrack  dm      94  amoeba bbt      e3  dos r/o          b  W95 FAT32        51  OnTrack DM6 Aux 9f  BSD/OS           e4  SpeedStor       c   w95 fat32  (LBA)  52  CP/M             a0  IBM Thinkpad hi eb  BeOS fs          e  W95 FAT16  (LBA)  53  OnTrack DM6  aux a5  freebsd         ee  gpt              f  w95 ext ' d  (LBA)  54  ontrackdm6      a6  openbsd          ef  EFI  (fat-12/16/10  opus             55  EZ-Drive        a7   Nextstep        f0  linux/pa-risc b11  hidden  FAT12    56  Golden Bow      a8   Darwin UFS      f1  SpeedStor       12  compaq diagnost 5c  priam edisk     a9   NetBSD          f4  SpeedStor       14  Hidden FAT16 <3 61  SpeedStor        ab  darwin boot     f2  dos secondary   16  hidden fat16    63  gnu hurd or sys  af  hfs / hfs+      fb  vmware vmfs     17  Hidden HPFS/NTF 64  Novell Netware   b7  bsdi fs         fc  vmware  Vmkcore 18  ast smartsleep  65  novell netware  b8   bsdi swap       fd  linux raid auto1b   Hidden W95 FAT3 70  DiskSecure Mult bb  Boot  Wizard hid fe  lanstep        1c  hidden  w95 fat3 75 &nbsP pc/ix           be  solaris boot     ff  bbt            1e   hidden w95 fat1 80  old minix      hex  code  (type l to list all codes): 82changed type of  partition  ' Linux '  to  ' Linux swap / solaris ' command  (m for help):  pdisk /dev/vdb: 10.7 gb, 10737418240 bytes, 20971520 sectorsunits  = sectors of 1 * 512 = 512 bytesSector size  (logical/ Physical): 512 bytes / 512 bytesi/o size  (minimum/optimal): 512  bytes / 512 bytesdisk label type: dosdisk identifier: 0x000ccf0e    device&nbSp boot      start         end       Blocks   Id  System/dev/vdb1             2048     4196351      2097152   82  Linux swap / SolarisCommand  (m for &NBSP;HELP):  wqthe partition table has been altered! Calling ioctl ()  to re-read partition table. Syncing disks
[Email protected] ~]# Partprobe # #同步分区表 [[email protected] ~]# MKSWAP/DEV/VDB1 # #将分区格式化为swap [email protected] ~]# Swapon-a/DEV/VDB1 # #添加swap分区 [[email protected] ~]# Swapon-s # #查看swap分区Filename Type s Ize used PRIORITY/DEV/VDB1 partition 2097148 0-1

Permanently add swap

CAT/ETC/FSTAB/DEV/VDB1 swap swap Defaults,pri=1 0 0

PRI represents priority

2: When a file is used as a swap partition

DD If=/dev/zero of=/swapfile bs=1m count=1000 # #创建一个1G大小的文件

Mkswap/swapfile # #格式化为swap类型

Swapon-a/swapfile # #临时添加到swap里

3: Delete swap partition

Vim/etc/fstab # #删除此文件中添加的swap行
Swapoff/swapfile # #断开swap文件链接
SWAPOFF/DEV/VDB1 # #断开swap磁盘链接
Rm-rf/swapfile # #删除文件
FDISK/DEV/VDB # #删除磁盘分区
Partprobe # #同步分区表

This article is from the "12462896" blog, please be sure to keep this source http://12472896.blog.51cto.com/12462896/1953838

Management of swap partitions in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.