Linux中Swap(即:交换分区),类似于Windows的虚拟内存,就是当内存不足的时候,把一部分硬盘空间虚拟成内存使用,从而解决内存容量不足的情况。
交换分区的创建有两种方式:

1.使用实体分区创建swap

[[email protected] ~]# fdisk /dev/sdb
Welcome 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): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  120G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  119G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0  3.8G  0 lvm  [SWAP]
  └─centos-home 253:2    0 65.2G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
sr0              11:0    1  9.6G  0 rom  
[[email protected] ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 10484732 KiB
no label, UUID=9239bf77-27b3-44b1-9a52-7493f3403c2d
[[email protected] ~]# blkid /dev/sdb1
/dev/sdb1: UUID="9239bf77-27b3-44b1-9a52-7493f3403c2d" TYPE="swap" 
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        188M        1.4G        9.5M        258M        1.5G
Swap:          3.7G          0B        3.7G
[[email protected] ~]# swapon /dev/sdb1
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        196M        1.4G        9.5M        258M        1.5G
Swap:           13G          0B         13G
[[email protected] ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	3932156	0	-2
/dev/sdb1                              	partition	10484732	0	-3
-------关闭swap分区操作------
[[email protected] ~]# swapoff /dev/sdb1
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        189M        1.4G        9.5M        258M        1.5G
Swap:          3.7G          0B        3.7G
[[email protected] ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	3932156	0	-2

2.使用文件创建swap

[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        189M        1.4G        9.5M        258M        1.5G
Swap:          3.7G          0B        3.7G
[[email protected] ~]# dd if=/dev/zero of=/tmp/swap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.90806 s, 182 MB/s
[[email protected] ~]# ll -h /tmp/swap 
-rw-r--r--. 1 root root 1.0G Jul  8 14:33 /tmp/swap
[[email protected] ~]# swapon /tmp/swap 
[[email protected] ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	3932156	0	-2
/tmp/swap                              	file	1048572	0	-3
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        189M        341M        9.5M        1.3G        1.5G
Swap:          4.7G          0B        4.7G
-------关闭swap分区操作------
[[email protected] ~]# swapoff /tmp/swap 
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        188M        342M        9.5M        1.3G        1.5G
Swap:          3.7G          0B        3.7G

TIPS:最后记得设置开机自动挂载分区,关闭时也需注释掉新增配置(UUID执行blkid查找)

[[email protected] ~]# vim /etc/fstab
UUID="9239bf77-27b3-44b1-9a52-7493f3403c2d"     swap    swap      defaults        0 0

扩展:

swap分区使用优先级配置

1.查看当前swappiness值
swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间
swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面

[[email protected] ~]# cat /proc/sys/vm/swappiness

2.修改swappiness值为100(临时修改,重启后即还原为默认值)

[[email protected] ~]# sysctl vm.swappiness=100

3.永久修改swappiness默认值(重启生效)

[[email protected] ~]# vim /etc/sysctl.conf
vm.swappiness=100

Reference URL:《鸟哥的linux私房菜》