使用磁盘为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。swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。现在服务器的内存动不动就是上百G,所以我们可以把这个参数值设置的低一些,让操作系统尽可能的使用物理内存,降低系统对swap的使用,从而提高系统的性能。
二.使用磁盘为Linux添加swap
查看磁盘及分区信息
[root@ip---- ~]# fdisk -l Disk /dev/xvda: 193.3 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x000da64d Device Boot Start End Blocks Id System
/dev/xvda1 * Linux Disk /dev/mapper/docker-:--pool: 107.4 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes Disk /dev/xvdf: MB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
添加新的分区
[root@ip---- ~]# fdisk /dev/xvdf
Welcome to fdisk (util-linux 2.23.). 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 action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only) Command (m for help): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p
Partition number (-, default ):
First sector (-, default ): (直接回车)
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ): (直接回车)
Using default value
Partition of type Linux and of size GiB is set Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.
查看磁盘及分区信息
[root@ip---- ~]# fdisk -l Disk /dev/xvda: 193.3 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x000da64d Device Boot Start End Blocks Id System
/dev/xvda1 * Linux Disk /dev/mapper/docker-:--pool: 107.4 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes Disk /dev/xvdf: MB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x8fb93abe Device Boot Start End Blocks Id System
/dev/xvdf1 Linux
可以看到/dev/xvdf1是新创建的分区,注意这里fdisk ID的编号。 这里显示的83,它代表这个分区是ext的分区,这个是不能用来做swap的。我们继续更改
[root@ip---- ~]# fdisk /dev/xvdf
Welcome to fdisk (util-linux 2.23.). 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) Empty NEC DOS Minix / old Lin bf Solaris
FAT12 Hidden NTFS Win Linux swap / So c1 DRDOS/sec (FAT-
XENIX root Plan Linux c4 DRDOS/sec (FAT-
XENIX usr 3c PartitionMagic OS/ hidden C: c6 DRDOS/sec (FAT-
FAT16 <32M Venix Linux extended c7 Syrinx
Extended PPC PReP Boot NTFS volume set da Non-FS data
FAT16 SFS NTFS volume set db CP/M / CTOS / .
HPFS/NTFS/exFAT 4d QNX4.x Linux plaintext de Dell Utility
AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
AIX bootable 4f QNX4.x 3rd part Amoeba e1 DOS access
a OS/ Boot Manag OnTrack DM Amoeba BBT e3 DOS R/O
b W95 FAT32 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
OPUS EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
Hidden FAT12 Golden Bow a8 Darwin UFS f1 SpeedStor
Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
Hidden FAT16 < SpeedStor ab Darwin boot f2 DOS secondary
Hidden FAT16 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
Hidden HPFS/NTF Novell Netware b7 BSDI fs fc VMware VMKCORE
AST SmartSleep Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 Old Minix Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x 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---- ~]# fdisk -l Disk /dev/xvda: 193.3 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x000da64d Device Boot Start End Blocks Id System
/dev/xvda1 * Linux Disk /dev/mapper/docker-:--pool: 107.4 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes Disk /dev/xvdf: MB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0x8fb93abe Device Boot Start End Blocks Id System
/dev/xvdf1 Linux swap / Solaris
格式化swap分区:
[root@rhce /]# mkswap /dev/xvdf1
Setting up swapspace version , size = KiB
no label, UUID=39c73cf6--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---- ~]# swapon -s
将/dev/xvdf1添加/etc/fstab文件,系统启动自动加载swap
[root@ip---- ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Jul ::
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(), findfs(), mount() and/or blkid() for more info
#
LABEL=centos7_root / xfs defaults
UUID=39c73cf6--4a78-83ec-35089f0eebcb swap swap defaults
使用磁盘为Linux添加swap的更多相关文章
- linux添加swap分区【转】
概述 添加交换分区主要是因为安装oracle时碰到交换分区太小时无法安装的情况,这时候就需要添加交换分区了. 操作简介 增加swap分区方法: 1.新建磁盘分区作为swap分区 2.用文件作为swap ...
- linux添加swap
一. 起因 发现云服务器重装后没有swap分区 于是我们分出一些物理内存做swap分区.. 二. 经过 (1)创建块文件 sudo dd if=/dev/zero of=/tmp/big_swap b ...
- linux 添加 swap
1)在linux下,首先,查看内存和swap大小: [root@rhel6 usr]# free -m total used free sha ...
- Linux添加swap分区
swap分区的作用为当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用,那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...
- 磁盘管理 之 parted命令添加swap,文件系统
第1章 磁盘管理 1.1 必须要了解的. 1.1.1 ps aux 命令中 RSS 与VSZ的含义 rss 进程占用的物理内存的大小 单位:kb : vsz 进程占用的虚拟的内存大小(物理内存+swa ...
- LINUX添加磁盘
博客网站原因,图片不能很好插入,望谅解 第一步:设置加载硬盘.由于是虚拟硬盘大小,不会一开始直接占用全部空间,使用多少占用多少,为了避免后期磁盘满前期设计大容量 第二步加载查看磁盘: 再执行fdisk ...
- linux开启swap(磁盘缓存)操作
转载 2014年04月26日 14:41:15 4470 由于工作需要,要帮助同事查看linux服务器的缓存开启情况,经过查找资料,可确定通过以下方法确定Linux磁盘缓存是否已开启. 1.命令行下执 ...
- Virtualbox中Linux添加新磁盘并创建分区
原文:https://www.linuxidc.com/Linux/2017-01/139616.htm ----------------------------------------------- ...
- 阿里云服务器linux主机如何添加swap分区
为什么要添加Swap分区?swap分区,即交换区,作用为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作 ...
随机推荐
- 第九篇 SQL Server安全透明数据加密
本篇文章是SQL Server安全系列的第九篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
- python get方法
dics.get(k,d)get相当于一条if...else...语句.如果参数k在字典dics中,字典将返回dics[k];返回参数d.例子 >>> l = {:, :} > ...
- Jboss消息 异常
org.jboss.mq.SpyJMSException: Could not update message: 1 msg=13 hard STORED PERSISTENT queue=QUEUE. ...
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- oracle调整表中列顺序
有一个哥们提出一个问题: 有个表,创建时候的列顺序是a,b,c 如何使用select * 的时候,让列的显示顺序是a,c,b 而且任性地必须使用select *来查询,且不能重建表. 假设有个表tes ...
- uiwebview 兼容性 - IOS8及以上 WKWebView
@import WKWebView; WKWebView *webView = [[WKWebView alloc]init......]; 使用. WKWebView兼容 IOS 及 OSX.IOS ...
- JMX超详细解读
一.JMX的定义 JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架.JMX是一套标准的代理和服务,实际上,用户可以在任何Java应用程序中使用这些代理和 ...
- Cocoapods注意点
1 安装和升级$ sudo gem install cocoapods $ pod setup 2 更换为taobao的源 $ gem sources -r https://rubygems.org/ ...
- PostgreSQL Replication之第十三章 使用PL/Proxy扩展(3)
13.3 聪明地扩展与处理集群 建立集群不是您面临的唯一任务.如果所有的事情都做完了并且系统已经运行了,您可能需要到处调整配置. 13.3.1 添加和移动分区 一旦一个集群启动并运行,您可能会发现您的 ...
- 在TextView上加上下划线或中划线
在做商城项目是需要用到原价格以及降价后的价格,不可避免用到下划线或中划线 textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下 ...