交换分区swap:
当cpu使用的内存不足时,就会暂时占用硬盘一部分空间来存储内存信息,这部分空间就是交换分区。
创建新的交换分区 swap:
1 新建分区,通过设置Id,建立swap分区
2 格式化swap分区 mkswap 设备名称
3 启动swap: swapon 设备名称
4 查看swap:free命令
mkswap命令:通过设备或文件,设置一个交换分区swap格式
mkswap [options] device [size]
参数:
-c:创建交换分区前,检测是否有坏块
-f,--force:强制创建
-L,--label 'label':根据指定label创建
-U,--uuid UUID: 根据uuid创建
free命令:查看内存memory和交换分区swap的使用状态
free [options]
-m: 以MB为单位
-g: 以GB为单位-h: 以易读的方式显示容量
swapon, swapoff 命令:
启用和禁用设备和文件作为交换分区
(enable/disable devices and files for paging and swapping)
swapon:启用交换分区
swapon [option] [DEVICE] -a: 激活所有交换分区 -p PRIORITY: 设定其优先级;优先使用 -v:显示详细信息swapoff:禁用交换分区
swapoff [option] [DEVICE]-a: 禁用所有交换分区
-v:显示详细信息
示例:
#1 创建新分区swap[root@localhost ~]# fdisk /dev/sdaWelcome 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): nAll primary partitions are in useAdding logical partition 9First sector (107507712-251658239, default 107507712): Using default value 107507712Last sector, +sectors or +size{K,M,G} (107507712-251658239, default 251658239): +512MPartition 9 of type Linux and of size 512 MiB is setCommand (m for help): pDisk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c2314 Device Boot Start End Blocks Id System..../dev/sda6 82337792 86532095 2097152 82 Linux swap / Solaris/dev/sda7 61853696 61855743 1024 83 Linux/dev/sda8 86534144 107505663 10485760 83 Linux/dev/sda9 107507712 108556287 524288 83 LinuxPartition table entries are not in disk orderCommand (m for help): tPartition number (1-9, default 9): 9Hex 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 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 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/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c2314 Device Boot Start End Blocks Id System....../dev/sda6 82337792 86532095 2097152 82 Linux swap / Solaris/dev/sda7 61853696 61855743 1024 83 Linux/dev/sda8 86534144 107505663 10485760 83 Linux/dev/sda9 107507712 108556287 524288 82 Linux swap / SolarisPartition table entries are not in disk orderCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.#2 格式化swap分区[root@localhost ~]# mkswap /dev/sda9Setting up swapspace version 1, size = 524284 KiBno label, UUID=7362fb3d-4bda-41d4-bef5-ba238de58aa2# 查看内存信息[root@localhost ~]# free -h total used free shared buff/cache availableMem: 979M 127M 704M 6.7M 147M 703MSwap: 2.0G 0B 2.0G# 3 加载swap分区[root@localhost ~]# swapon /dev/sda9[root@localhost ~]# free -h total used free shared buff/cache availableMem: 979M 128M 704M 6.7M 147M 702MSwap: 2.5G 0B 2.5G#停用指定swap分区[root@localhost ~]# swapoff -v /dev/sda9swapoff /dev/sda9#启用指定swap分区[root@localhost ~]# swapon -v /dev/sda9swapon /dev/sda9swapon: /dev/sda9: found swap signature: version 1, page-size 4, same byte orderswapon: /dev/sda9: pagesize=4096, swapsize=536870912, devsize=536870912
如有错误,敬请指正!
谢谢!