linux開啟swap(磁碟快取)操作
1.命令列下執行free命令,當顯示如下紅框內的資訊(swap)時,說明swap處於啟用狀態
此時注意,開啟時total和free處均有值。
2.命令列下執行swapoff -a,關閉swap
3.此時執行free命令
此時注意,並閉時total和free處均為0,由此可知swap已經關閉。
4.附swapon、swapoff引數說明
swapon
Usage:
swapon -a [-e] [-v] [-f] enable all swaps from /etc/fstab
swapon [-p priority] [-v] [-f] <special> enable given swap
swapon -s display swap usage summary
swapon -h display help
swapon -V display version
The <special> parameter:
{-L label | LABEL=label} LABEL of device to be used
{-U uuid | UUID=uuid} UUID of device to be used
<device> name of device to be used
<file> name of file to be used
swapoff
Usage:
swapoff -a [-v] disable all swaps
swapoff [-v] <special> disable given swap
swapoff -h display help
swapoff -V display version
The <special> parameter:
{-L label | LABEL=label} LABEL of device to be used
{-U uuid | UUID=uuid} UUID of device to be used
<device> name of device to be used
<file> name of file to be used
Android開發板實戰:
手機終端su確認後輸入:busybox free .可以檢視Sawp分割槽情況。
=============================================================================
詳解
=============================================================================
一.SWAP 說明
1.1 SWAP 概述
當系統的實體記憶體不夠用的時候,就需要將實體記憶體中的一部分空間釋放出來,以供當前執行的程式使用。那些被釋放的空間可能來自一些很長時間沒有什麼操作的程式,這些被釋放的空間被臨時儲存到Swap空間中,等到那些程式要執行時,再從Swap中恢復儲存的資料到記憶體中。這樣,系統總是在實體記憶體不夠時,才進行Swap交換。
這個是SWAP 交換分割槽的作用。 實際上,我們更關注的應該是SWAP分割槽的大小問題。 設定多大才是最優的。
一般來說可以按照如下規則設定swap大小:
4G以內的實體記憶體,SWAP 設定為記憶體的2倍。
4-8G的實體記憶體,SWAP 等於記憶體大小。
8-64G 的實體記憶體,SWAP 設定為8G。
64-256G實體記憶體,SWAP 設定為16G。
實際上,系統中交換分割槽的大小並不取決於實體記憶體的量,而是取決於系統中記憶體的負荷,所以在安裝系統時要根據具體的業務來設定SWAP的值。
1.2 系統在什麼情況下才會使用SWAP?
實際上,並不是等所有的實體記憶體都消耗完畢之後,才去使用swap的空間,什麼時候使用是由swappiness 引數值控制。
[[email protected] ~]# cat /proc/sys/vm/swappiness
60
該值預設值是60.
swappiness=0的時候表示最大限度使用實體記憶體,然後才是 swap空間,
swappiness=100的時候表示積極的使用swap分割槽,並且把記憶體上的資料及時的搬運到swap空間裡面。
現在伺服器的記憶體動不動就是上百G,所以我們可以把這個引數值設定的低一些,讓作業系統儘可能的使用實體記憶體,降低系統對swap的使用,從而提高系統的效能。
1.3 如何修改swappiness引數?
--臨時性修改:
[[email protected] ~]# sysctl vm.swappiness=10
vm.swappiness = 10
[[email protected] ~]# cat /proc/sys/vm/swappiness
10
這裡我們的修改已經生效,但是如果我們重啟了系統,又會變成60.
--永久修改:
在/etc/sysctl.conf 檔案裡新增如下引數:
vm.swappiness=10
或者:
[[email protected] ~]# echo 'vm.swappiness=10' >>/etc/sysctl.conf
儲存,重啟,就生效了。
二.管理SWAP
2.1 檢視系統當前SWAP 空間大小
[[email protected] ~]# free -m
total used free shared buffers cached
Mem: 1954 1923 31 0 21 1345
-/+ buffers/cache: 555 1399
Swap: 1999 21 1978
2.2 釋放SWAP 空間
假設我們的系統出現了效能問題,我們通過vmstat命令看到有大量的swap,而我們的實體記憶體又很充足,那麼我們可以手工把swap 空間釋放出來。讓程序去使用實體記憶體,從而提高效能。
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# 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 的使用
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 22272 -1
[[email protected] ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 2047992 22272 -1
[[email protected] ~]#
swapon –s 等於 cat/proc/swaps
--關閉swap 交換分割槽:
[[email protected] ~]# swapoff/dev/sda2
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
--檢視swap的使用情況:
[[email protected] ~]# free -m
total used free shared buffers cached
Mem: 1954 1939 15 0 21 1343
-/+ buffers/cache: 573 1381
Swap: 0 0 0
--啟用swap分割槽:
[[email protected] ~]# swapon /dev/sda2
--驗證狀態:
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 0 -1
這裡/dev/sda2是我們在安裝作業系統時劃分的磁碟分割槽。實際上,我們也可以使用檔案來做為交換分割槽。具體後面會演示。
我們上面寫的是具體名稱,也可是使用swapoff -a程序,演示如下:
[[email protected] ~]# swapoff -a
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
[[email protected] ~]# swapon -a
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2047992 0 -1
[[email protected] ~]#
我們檢視/etc/fstab 檔案,我們的swap 也配置到開啟自動啟動了。
[[email protected] ~]# 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 分割槽來實現的。
2.3 使用檔案來作為SWAP 交換分割槽
我們這裡使用檔案新增一個交換區,具體操作如下:
--在根目錄下生成一個檔案:swap-file,大小1G:
[[email protected] 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
[[email protected] u01]# cd /
[[email protected] /]# 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
[[email protected] /]#
--將生成的檔案格式化成交換分割槽:
[[email protected] /]# 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分割槽並檢視狀態:
[[email protected] /]# swapon /swap-file
[[email protected] /]# 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
2.4 使用磁碟新增swap
先劃分一個1G的磁碟分割槽出來:
[[email protected] /]# 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.
[[email protected] /]# 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 分割槽:
[[email protected] ~]# 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.
[[email protected] ~]#
--格式化swap分割槽:
[[email protected] /]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID= bc913e6b-209a-49a7-b561-9b8c57294681
[[email protected] /]# 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:
[[email protected] ~]# 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"
[[email protected] ~]#
這裡的型別已經改變,我們可以嘗試手工的啟動swap:
[[email protected] ~]# swapon /dev/sdc1
[[email protected] ~]# 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都新