![738de6108591baa8dc0ddfb757e34a30.png](https://img-blog.csdnimg.cn/img_convert/738de6108591baa8dc0ddfb757e34a30.png)
前言
在Linux系统中,swap交换分区的作用类似于Windows系统中“虚拟内存”,当有程序被调入内存后,但是该程序又不是常被CPU所取用时,那么这些不常被使用的程序将会被放到硬盘swap交换分区当中。
目录
一、swap交换分区描述
二、swap分区大小设置
三、swap交换分区的优劣
四、相关命令
五、示例
六、总结
七、思维导图
一、swap交换分区描述
1、在Linux系统中:
- swap交换分区的作用类似于Windows系统中“虚拟内存”;
- 当有程序被调入内存后,但是该程序又不是常被CPU所取用时,那么这些不常被使用的程序将会被放到硬盘的swap交换分区当中;
- 将速度较快的内存空间释放给真正需要的程序使用;
- 避免因为物理内存不足而造成的系统效能低的问题;
- 如果系统没有swap交换分区,或者现有交换分区的容量不够用时,可扩展swap交换分区。
2、扩展swap交换分区的方式:
- 以磁盘分区的方式扩展swap交换分区;
- 以镜像文件的方式扩展swap交换分区。
二、swap分区大小设置
系统的swap分区大小设置为多少才算是最优呢?
关于此类问题,具体还应该根据系统实际情况和内存的负荷综合考虑~
- 物理内存 ≤ 4GB:swap设置为内存的2倍;
- 4BG ≤ 物理内存 ≤ 8GB:swap等于内存大小;
- 8GB ≤ 物理内存 ≤ 64GB:swap设置为8GB;
三、swap交换分区的优劣
在Linux中,相比文件系统中的一个常规文件或独立分区作为swap交换空间,交换分区要更快一些;但是和RAM相比,swap交换分区的性能就显得弱一些。
1、优点:
- 在内存占满的时候,swap交换分区会作为溢出空间帮助内存工作;
- 可以将不常用的项目从RAM移到swap交换分区当中,以提高RAM的工作效率;
- 用户可以根据自己的需求停止或激活swap分区。
2、缺点:
- 需要占用硬盘空间作为swap交换分区;
- 增加硬盘磨损;
- 不一定能提高性能。
四、相关命令
1、mkswap 分区设备名
- 将指定的分区格式化为swap交换文件系统。
2、swapon 交换分区设备名|-a
- 启动指定的交换分区或所有交换分区。
3、swapoff 交换分区设备名|-a
- 禁用指定的交换分区或所有交换分区。
4、swapon -s
- 查看交换分区的使用情况。
5、free -m
- 以兆字节为单位显示物理内存、交换分区的使用情况。
五、示例
- 要求:在磁盘/dev/sdb上用一个新建的分区扩展swap交换分区。
# 1、创建容量为256MB的主分区。[root@localhost ~]# fdisk /dev/sdbWelcome 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 (2 primary, 0 extended, 2 free) e extendedSelect (default p): pPartition number (3,4, default 3): 3First sector (11536384-41943039, default 11536384): 11536384Last sector, +sectors or +size{K,M,G} (11536384-41943039, default 41943039): +256MPartition 3 of type Linux and of size 256 MiB is set# 2、使用“t”指令转换分区类型,可以使用“L”指令显示所有分区类型的十六进制代码表。Command (m for help): tPartition number (1-3, default 3):3Hex code (type L to list all codes): 82Changed type of partition 'Linux' to 'Linux swap / Solaris'# 3、保存并退出。Command (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# 4、通知系统分区表的变化[root@localhost ~]# partprobe /dev/sdb # 5、格式化交换分区 [root@localhost ~]# mkswap /dev/sdb3Setting up swapspace version 1, size = 262140 KiBno label, UUID=45c9d92d-e0b0-418d-a60e-9af75a8d6ddd# 6、显示扩展前已启用的交换分区的大小[root@localhost ~]# free -m total used free shared buff/cache availableMem: 1819 179 1404 9 235 1445Swap: 2047 0 2047 # 7、手工临时启用新添加的交换分区 [root@localhost ~]# swapon /dev/sdb3 # 8、查看交换分区的使用情况[root@localhost ~]# swapon -sFilenameTypeSizeUsedPriority/dev/dm-1 partition20971480-2/dev/sdb3 partition2621400-3 # 9、显示扩展后已启用的交换分区的大小[root@localhost ~]# free -m total used free shared buff/cache availableMem: 1819 179 1404 9 235 1445Swap: 2303 0 2303# 10、如果要添加永久挂载,可以使用blkid查询新交换分区的UUID,写入/etc/fstab文件即可[root@dyzx ~]# blkid /dev/sdb1//查看新添加交换分区的UUID [root@dyzx ~]# vim /etc/fstab//省略若干行,//在文件末尾添加以下一行:UUID=944c419b-a15b-40c7-9e97-8b750cc008db swap swap defaults 0 0
六、总结
- swap交换分区的作用类似于Windows系统中“虚拟内存”
- 扩展交换分区的两种方式:磁盘分区、镜像文件
- swap交换分区的相关命令:
![08b2eda15ee9d785bf8c395f6d76a158.png](https://img-blog.csdnimg.cn/img_convert/08b2eda15ee9d785bf8c395f6d76a158.png)
七、思维导图
![1d0d530fd13c65f901907ea7ca38a2c1.png](https://img-blog.csdnimg.cn/img_convert/1d0d530fd13c65f901907ea7ca38a2c1.png)