SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。交换分区的创建过程很简单。为了让大家更明显地感受交换分区空间的变化,这里取出一个大小为 3GB 的主分区作为交换分区资源。
目前swap分区大小:
[root@bogon ~]# free -m
total used free shared buffers cached
Mem: 475 244 231 4 0 73
-/+ buffers/cache: 170 305
Swap: 2047 0 2047
[root@bogon ~]# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0a6771d1.
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):
First sector (2048-31457279, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): +3G
Partition 1 of type Linux and of size 3 GiB is set
Command (m for help): p
Disk /dev/sdb: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0a6771d1
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
使用 SWAP 分区专用的格式化命令 mkswap,对新建的主分区进行格式化操作:
[root@bogon ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 3145724 KiB
no label, UUID=a2fd4938-5791-4010-adff-bb55ceb9a002
使用 swapon 命令把准备好的 SWAP 分区设备正式挂载到系统中。我们可以使用 free -m 命令查看交换分区的大小变化,由2047变为5119
[root@bogon ~]# swapon /dev/sdb1
[root@bogon ~]# free -m
total used free shared buffers cached
Mem: 475 247 228 4 0 73
-/+ buffers/cache: 172 303
Swap: 5119 0 5119
为了能够让新的交换分区设备在重启后依然生效,需要修改配置文件
[root@bogon ~]# vim /etc/fstab