當系統的物理內存不夠用的時候,就須要將物理內存中的一部分空間釋放出來,以供當前運行的程序使用。那些被釋放的空間可能來自一些很長時間沒有什麼操做的程序,這些被釋放的空間被臨時保存到Swap空間中,等到那些程序要運行時,再從Swap中恢復保存的數據到內存中。這樣,系統老是在物理內存不夠時,才進行Swap交換。linux
這個是SWAP 交換分區的做用。 實際上,咱們更關注的應該是SWAP分區的大小問題。 設置多大才是最優的。服務器
通常來講能夠按照以下規則設置swap大小:app
4G之內的物理內存,SWAP 設置爲內存的2倍。ide
4-8G的物理內存,SWAP 等於內存大小。性能
8-64G 的物理內存,SWAP 設置爲8G。測試
64-256G物理內存,SWAP 設置爲16G。ui
實際上,系統中交換分區的大小並不取決於物理內存的量,而是取決於系統中內存的負荷,因此在安裝系統時要根據具體的業務來設置SWAP的值。this
實際上,並非等全部的物理內存都消耗完畢以後,纔去使用swap的空間,何時使用是由swappiness 參數值控制。spa
[root@rhce ~]# cat /proc/sys/vm/swappiness操作系統
60
該值默認值是60.
swappiness=0的時候表示最大限度使用物理內存,而後纔是 swap空間,
swappiness=100的時候表示積極的使用swap分區,而且把內存上的數據及時的搬運到swap空間裏面。
如今服務器的內存動不動就是上百G,因此咱們能夠把這個參數值設置的低一些,讓操做系統儘量的使用物理內存,下降系統對swap的使用,從而提升系統的性能。
--臨時性修改:
[root@rhce ~]# sysctl vm.swappiness=10
vm.swappiness = 10
[root@rhce ~]# cat /proc/sys/vm/swappiness
10
這裏咱們的修改已經生效,可是若是咱們重啓了系統,又會變成60.
--永久修改:
在/etc/sysctl.conf 文件裏添加以下參數:
vm.swappiness=10
或者:
[root@rhce ~]# echo 'vm.swappiness=10' >>/etc/sysctl.conf
保存,重啓,就生效了。
[root@dave ~]# free -m
total used free shared buffers cached
Mem: 1954 1923 31 0 21 1345
-/+ buffers/cache: 555 1399
Swap: 1999 21 1978
假設咱們的系統出現了性能問題,咱們經過vmstat命令看到有大量的swap,而咱們的物理內存又很充足,那麼咱們能夠手工把swap 空間釋放出來。讓進程去使用物理內存,從而提升性能。
[root@dave ~]# vmstat 1 5
procs -----------memory---------- ---swap-------io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 22272 32620 22032 1378312 0 0 33 38 0 41 1 2 96 0 0
0 0 22272 32612 22032 1378340 0 0 0 0 902 1627 0 5 95 0 0
0 0 22272 32612 22032 1378340 0 0 0 0 905 1636 1 8 91 0 0
0 0 22272 32612 22032 1378340 0 0 0 32 907 1616 0 6 94 0 0
0 0 22272 32612 22032 1378340 0 0 0 0 924 1651 0 8 92 0 0
[root@dave ~]#
[root@dave ~]# free -m
total used free shared buffers cached
Mem: 1954 1923 31 0 21 1345
-/+ buffers/cache: 555 1399
Swap: 1999 21 1978
注意:free命令默認單位爲k, -m 單位爲M。 咱們這裏的swap使用了21M的空間。
--查看當前swap 的使用
[root@dave ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 22272 -1
[root@dave ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 2047992 22272 -1
[root@dave ~]#
swapon –s 等於 cat/proc/swaps
--關閉swap 交換分區:
[root@dave ~]# swapoff/dev/sda2
[root@dave ~]# swapon -s
Filename Type Size Used Priority
--查看swap的使用狀況:
[root@dave ~]# free -m
total used free shared buffers cached
Mem: 1954 1939 15 0 21 1343
-/+ buffers/cache: 573 1381
Swap: 0 0 0
--啓用swap分區:
[root@dave ~]# swapon /dev/sda2
--驗證狀態:
[root@dave ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 0 -1
這裏/dev/sda2是咱們在安裝操做系統時劃分的磁盤分區。實際上,咱們也可使用文件來作爲交換分區。具體後面會演示。
咱們上面寫的是具體名稱,也但是使用swapoff -a進程,演示以下:
[root@dave ~]# swapoff -a
[root@dave ~]# swapon -s
Filename Type Size Used Priority
[root@dave ~]# swapon -a
[root@dave ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 0 -1
[root@dave ~]#
咱們查看/etc/fstab 文件,咱們的swap 也配置到開啓自動啓動了。
[root@dave ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Aug 15 00:09:26 2012
#
# Accessible filesystems, by reference, aremaintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info
#
UUID=beea0820-3ac3-4551-9bf0-1b2a462d3ab4 / ext4 defaults 1 1
UUID=e5ec9543-143f-4e3b-b8a7-4fa05b01836a/boot ext4 defaults 1 2
UUID=b256c0bb-9000-456b-b9eb-18239b5df5ddswap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
簡單的說:
(1)ext分區是否啓用由mount及umount控制。
(2)swap分區是否啓動,由swapon及swapoff控制。
咱們對swap 空間的釋放,也是經過關閉swap分區,在啓動swap 分區來實現的。
咱們這裏使用文件添加一個交換區,具體操做以下:
--在根目錄下生成一個文件:swap-file,大小1G:
[root@dave u01]# dd if=/dev/zero of=/swap-filebs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.91518 s, 182MB/s
[root@dave u01]# cd /
[root@dave /]# ls
bin cgroup etc lib lost+found misc net proc sbin srv sys u01 usr
boot dev home lib64 media mnt opt root selinux swap-file tmp u02 var
[root@dave /]#
--將生成的文件格式化成交換分區:
[root@dave /]# mkswap /swap-file
mkswap: /swap-file: warning: don't erase bootbitssectors
onwhole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=653bbeb5-4abb-4295-b110-5847e073140d
--這裏沒有分區的lable,只有一個UUID。
--啓動swap分區並查看狀態:
[root@dave /]# swapon /swap-file
[root@dave /]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 0 -1
/swap-file file 1048568 0 -2
這裏咱們就看到了2個swap。
可是這個只對當前有效,若是想下次重啓系統後還繼續有效,須要將配置寫入到/etc/fstab文件中。
在/etc/fstab文件中添加以下內容:
UUID=653bbeb5-4abb-4295-b110-5847e073140d swap swap defaults 0 0
或者:
/swap-file swap swap defaults 0 0
先劃分一個1G的磁盤分區出來:
[root@rhce /]# fdisk /dev/sdb
Device contains neither a valid DOS partitiontable, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier0x65edb587.
Changes will remain in memory only, until youdecide to write them.
After that, of course, the previous content won'tbe recoverable.
Warning: invalid flag 0x0000 of partition table 4will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It'sstrongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652,default 652): +1G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@rhce /]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000bcd24
DeviceBoot Start End Blocks Id System
/dev/sda1 * 1 128 1024000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 128 511 3072000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 511 2611 16874496 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x65edb587
DeviceBoot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
…
注意這裏fdisk ID的編號。 這裏顯示的83,它表明這個分區是ext的分區,這個是不能用來作swap的。
在fdisk 命令能夠看到具體數據的含義:
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linuxplaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 HiddenFAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaqdiagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 HiddenFAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 HiddenFAT16 64 Novell Netware af HFS/ HFS+ fb VMware VMFS
17 HiddenHPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 ASTSmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b HiddenW95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c HiddenW95 FAT3 80 Old Minix be Solaris boot ff BBT
1e HiddenW95 FAT1
ext的分區ID號:0x83
swap的分區ID號:0x82
--將/dev/sdb1分區改爲swap 分區:
[root@rhce ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It'sstrongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): h
h: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
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 codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS /.
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linuxplaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 HiddenFAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaqdiagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 HiddenFAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 HiddenFAT16 64 Novell Netware af HFS/ HFS+ fb VMware VMFS
17 HiddenHPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 ASTSmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b HiddenW95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c HiddenW95 FAT3 80 Old Minix be Solaris boot ff BBT
1e HiddenW95 FAT1
Hex code (type L to list codes):82
Changed system type of partition1 to 82 (Linux swap / Solaris)
--注意這裏更改磁盤分區的ID類型,若是不更在,在系統重啓時會根據ID來判斷磁盤的類型,那樣在使用時就會出現錯誤。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed witherror 16: Device or resource busy.
The kernel still uses the old table. The new tablewill be used at
the next reboot or after you run partprobe(8) orkpartx(8)
Syncing disks.
[root@rhce ~]#
--格式化swap分區:
[root@rhce /]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID= bc913e6b-209a-49a7-b561-9b8c57294681
[root@rhce /]# mkswap –L swap-disk/dev/sdc1
一個設置了Lable,一個沒設置。咱們在添加/etc/fstab時,但是用LABLE名稱,或者使用UUID來配置。 具體如上。
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x65edb587
DeviceBoot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 82 Linux swap / Solaris
Disk /dev/sdc: 5368 MB, 5368709120 bytes
181 heads, 40 sectors/track, 1448 cylinders
Units = cylinders of 7240 * 512 = 3706880 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
DeviceBoot Start End Blocks Id System
/dev/sdc1 1 291 1053400 82 Linux swap / Solaris
注意: 使用blkid 查看類型已經變成了swap:
[root@rhce ~]# blkid
/dev/sda1: UUID="935f7fb2-2ef4-486f-ae4e-265eaf9f580f"TYPE="ext4"
/dev/sda2:UUID="43343402-9188-4cdc-9c79-0e36ab737517" TYPE="swap"
/dev/sda3:UUID="7e49fb2c-6fd8-461a-95d9-65d1f3994160" TYPE="ext4"
/dev/sdb1:UUID="bc913e6b-209a-49a7-b561-9b8c57294681" TYPE="swap"
/dev/sdc1:LABEL="swap-disk"UUID="799b4379-ca59-4508-ad04-2d18847b3fd9" TYPE="swap"
[root@rhce ~]#
這裏的類型已經改變,咱們能夠嘗試手工的啓動swap:
[root@rhce ~]# swapon /dev/sdc1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
/dev/sdc1 partition 1053392 0 -3
這裏能夠直接啓用swap。
咱們這裏把/dev/dbb1和/dev/sdc1都添加/etc/fstab 文件,在重啓一下看看。 添加內容以下:
UUID=bc913e6b-209a-49a7-b561-9b8c57294681 swap swap defaults 0 0
LABEL=swap-disk swap swap defaults 0 0
重啓系統後查看:
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
/dev/sdc1 partition 1053392 0 -3
--對/dev/sdb1 進行測試:
[root@rhce ~]# swapoff /dev/sdb1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
[root@rhce ~]# swapon /dev/sdb1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
Swapoff 文件以後,刪除掉對應的空間,在從/etc/fstab刪除對應的記錄便可。