使用磁盘为Linux添加swap
一.SWAP 说明
1、SWAP 概述
当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。
以上是SWAP 交换分区的作用。 实际上,我们更关注的应该是SWAP分区的大小问题。 设置多大才是最优的。一般来说可以按照如下规则设置swap大小:
4G以内的物理内存,SWAP 设置为内存的2倍。
4-8G的物理内存,SWAP 等于内存大小。
8-64G 的物理内存,SWAP 设置为8G。
64-256G物理内存,SWAP 设置为16G。
实际上,系统中交换分区的大小并不取决于物理内存的量,而是取决于系统中内存的负荷,所以在安装系统时要根据具体的业务来设置SWAP的值。
2、系统在什么情况下才会使用SWAP?
实际上,并不是等所有的物理内存都消耗完毕之后,才去使用swap的空间,什么时候使用是由swappiness 参数值控制。
[root@rhce ~]# cat /proc/sys/vm/swappiness 60
该值默认值是60。swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。现在服务器的内存动不动就是上百G,所以我们可以把这个参数值设置的低一些,让操作系统尽可能的使用物理内存,降低系统对swap的使用,从而提高系统的性能。
二.使用磁盘为Linux添加swap
查看磁盘及分区信息
[root@ip-192-168-31-161 ~]# fdisk -lDisk /dev/xvda: 193.3 GB, 193273528320 bytes, 377487360 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: 0x000da64dDevice Boot Start End Blocks Id System /dev/xvda1 * 2048 377487359 188742656 83 LinuxDisk /dev/mapper/docker-202:1-385876097-pool: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 65536 bytes / 65536 bytesDisk /dev/xvdf: 4294 MB, 4294967296 bytes, 8388608 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
添加新的分区
[root@ip-192-168-31-161 ~]# fdisk /dev/xvdf 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 0x8fb93abe.Command (m for help): m Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)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-8388607, default 2048): (直接回车) Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): (直接回车) Using default value 8388607 Partition 1 of type Linux and of size 4 GiB is setCommand (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table. Syncing disks.
查看磁盘及分区信息
[root@ip-192-168-31-161 ~]# fdisk -lDisk /dev/xvda: 193.3 GB, 193273528320 bytes, 377487360 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: 0x000da64dDevice Boot Start End Blocks Id System /dev/xvda1 * 2048 377487359 188742656 83 LinuxDisk /dev/mapper/docker-202:1-385876097-pool: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 65536 bytes / 65536 bytesDisk /dev/xvdf: 4294 MB, 4294967296 bytes, 8388608 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: 0x8fb93abeDevice Boot Start End Blocks Id System /dev/xvdf1 2048 8388607 4193280 83 Linux
可以看到/dev/xvdf1是新创建的分区,注意这里fdisk ID的编号。 这里显示的83,它代表这个分区是ext的分区,这个是不能用来做swap的。我们继续更改
[root@ip-192-168-31-161 ~]# fdisk /dev/xvdf 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): l (list known partition types)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 b 11 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 auto 1b 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 Command (m for help): m Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris'Command (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table. Syncing disks.
查看磁盘及分区信息
[root@ip-192-168-31-161 ~]# fdisk -lDisk /dev/xvda: 193.3 GB, 193273528320 bytes, 377487360 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: 0x000da64dDevice Boot Start End Blocks Id System /dev/xvda1 * 2048 377487359 188742656 83 LinuxDisk /dev/mapper/docker-202:1-385876097-pool: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 65536 bytes / 65536 bytesDisk /dev/xvdf: 4294 MB, 4294967296 bytes, 8388608 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: 0x8fb93abeDevice Boot Start End Blocks Id System /dev/xvdf1 2048 8388607 4193280 82 Linux swap / Solaris
格式化swap分区:
[root@rhce /]# mkswap /dev/xvdf1 Setting up swapspace version 1, size = 1060252 KiB no label, UUID=39c73cf6-1848-4a78-83ec-35089f0eebcb [root@ip-192-168-31-161 ~]# blkid /dev/xvda1: LABEL="centos7_root" UUID="179c37d9-9ba3-47f2-ab50-cf76dec5ef8e" TYPE="xfs"
/dev/loop0: UUID="0c7e9760-c4df-4d5b-b0b6-b2f4487e757b" TYPE="ext4" /dev/mapper/docker-202:1-385876097-pool: UUID="0c7e9760-c4df-4d5b-b0b6-b2f4487e757b" TYPE="ext4" /dev/xvdf1: UUID="39c73cf6-1848-4a78-83ec-35089f0eebcb" TYPE="swap"
手工的启动swap
[root@ip-192-168-31-161 ~]# swapon /dev/xvdf1
[root@ip-192-168-31-161 ~]# swapon -s
将/dev/xvdf1添加/etc/fstab文件,系统启动自动加载swap
[root@ip-192-168-31-161 ~]# vi /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 8 16:48:19 2014 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # LABEL=centos7_root / xfs defaults 1 1 UUID=39c73cf6-1848-4a78-83ec-35089f0eebcb swap swap defaults 0 0
使用磁盘为Linux添加swap相关推荐
- linux交换分区目录,Linux添加swap交换分区
添加swap交换分区 SWAP即交换分区是一种类似于Windows系统虚拟内存的功能,将一部分硬盘空间虚拟成内存来使用,从而解决内存容量不足的情况,因为SWAP毕竟是用硬盘资源虚拟的,所以速度上比真实 ...
- linux添加swap虚拟内存
1.swap概述 Swap分区,即交换区,Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长 ...
- linux更改甜器名称,Linux添加swap分区
swap分区的作用为当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用,那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...
- linux swap 阀值,Linux 添加swap分区, 设定swap阈值
swap阈值设定1, 查看当前系统swap阈值 chunli@CentOS~$ cat /proc/sys/vm/swappiness 60 内存在使用到100-60=40%的时候,就开始出现有交换 ...
- suse linux增加新磁盘分区,Virtualbox中Linux添加新磁盘并创建分区
引言:我们常常在使用系统的时候突然发现,哎呦~~~我们的磁盘空间不够用啦!我遇到常见的就是数据库数据暴增,预留的空间没有啦,只好新添加磁盘,在VB虚拟机上就可以实现,往往苦于没有图文并茂的好资料,下面 ...
- 磁盘管理 之 parted命令添加swap,文件系统
第1章 磁盘管理 1.1 必须要了解的. 1.1.1 ps aux 命令中 RSS 与VSZ的含义 rss 进程占用的物理内存的大小 单位:kb : vsz 进程占用的虚拟的内存大小(物理内存+swa ...
- linux开启swap(磁盘缓存)操作
转载 2014年04月26日 14:41:15 4470 由于工作需要,要帮助同事查看linux服务器的缓存开启情况,经过查找资料,可确定通过以下方法确定Linux磁盘缓存是否已开启. 1.命令行下执 ...
- Linux下fdisk命令操作磁盘详解--添加、删除、转换分区
linux下fdisk命令操作磁盘详解--添加.删除.转换分区等 fdisk 操作硬盘的命令格式如下: [root@localhost beinan]# fdisk 设备 比如我们通过 fdisk - ...
- vmware中linux使用gparted调整磁盘大小并设置swap分区
vmware中linux使用gparted调整磁盘大小并设置swap分区 今天使用虚拟机的时候,发现虚拟机快满了,提示磁盘空间小,不得不扩充虚拟机空间. 1首先把vmware中的ubuntu关机,然后 ...
最新文章
- python3.6安装opencv-python3.6.5安装opencv3
- python程序员脱单攻略_作为一只程序员,如何脱单?
- 第三十九期:收藏 | 第一次有人把“分布式事务”讲的这么简单明了
- 1. 栈和队列的数组实现
- 基于动态混合高斯模型的商品价格模型算法
- Android实现登录
- python主线程和子线程_python 在threading中如何处理主进程和子线程的关系
- 2019swpuj2ee作业3
- 如何查看android虚拟机的目录及文件
- linux卸载qt5.6.2,请问该怎么把linux的Qt升级为5.6?
- Python单元测试框架之pytest -- fixtures
- 对象存储是什么?看过就明白了
- 学生管理系统IPO图_高校学生考勤管理系统
- 燕山大学概率论复习资料
- Oracle数据库初学者入门教程
- 51单片机模拟I2C读写M24LR04
- LIRO:紧耦合激光-惯性-测距里程计
- 我跑了香港六家银行,把境外开户的事整明白了
- 医学知识---之T1,T2,flair,stir
- 【labview】图表时间轴(数据+时间戳) +源程序