Swap交換分區概念

什麼是Linux swap space呢?我們先來看看下面兩段關於Linux swap space的英文介紹資料:

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

Linux內核為了提高讀寫效率與速度,會將文件在內存中進行緩存,這部分內存就是Cache Memory(緩存內存)。即使你的程序運行結束後,Cache Memory也不會自動釋放。這就會導致你在Linux系統中程序頻繁讀寫文件後,你會發現可用物理內存變少。當系統的物理內存不夠用的時候,就需要將物理內存中的一部分空間釋放出來,以供當前運行的程序使用。那些被釋放的空間可能來自一些很長時間沒有什麼操作的程序,這些被釋放的空間被臨時保存到Swap空間中,等到那些程序要運行時,再從Swap分區中恢復保存的數據到內存中。這樣,系統總是在物理內存不夠時,才進行Swap交換。

關於Swap分區,其實我們有很多疑問,如果能弄清楚這些疑問,那麼你對Swap的瞭解掌握就差不多了。如何查看Swap分區大小? Swap分區大小應該如何設置?系統在什麼時候會使用Swap分區? 是否可以調整? 如何調整Swap分區的大小?Swap分區有什麼優劣和要注意的地方? Swap分區是否必要?那麼我一個一個來看看這些疑問吧!

查看Swap分區大小

查看Swap分區的大小以及使用情況,一般使用free命令即可,如下所示,Swap大小為2015M,目前沒有使用Swap分區

[root@DB-Server ~]# free -m
total used free shared buffers cached
Mem: 1000 855 145 0 28 296
-/+ buffers/cache: 530 470
Swap: 2015 0 2015

另外我們還可以使用swapon命令查看當前swap相關信息:例如swap空間是swap partition,Swap size,使用情況等詳細信息

[root@DB-Server ~]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2064344 0 -1
[root@DB-Server ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda3 partition 2064344 0 -1
[root@DB-Server ~]#
Swap交換分區概念

Swap分區大小設置

系統的Swap分區大小設置多大才是最優呢? 關於這個問題,應該說只能有一個統一的參考標準,具體還應該根據系統實際情況和內存的負荷綜合考慮,像ORACLE的官方文檔就推薦如下設置,這個是根據物理內存來做參考的。

RAM

Swap Space

Up to 512 MB

2 times the size of RAM

Between 1024 MB and 2048 MB

1.5 times the size of RAM

Between 2049 MB and 8192 MB

Equal to the size of RAM

More than 8192 MB

0.75 times the size of RAM

另外在其它博客中看到下面一個推薦設置,當然我不清楚其怎麼得到這個標準的。是否合理也無從考證。可以作為一個參考。

4G以內的物理內存,SWAP 設置為內存的2倍。

4-8G的物理內存,SWAP 等於內存大小。

8-64G 的物理內存,SWAP 設置為8G。

64-256G物理內存,SWAP 設置為16G。

上下兩個標準確實也很讓人無所適從。我就有一次在一臺ORACLE數據庫服務器(64G的RAM),按照官方推薦設置了一個很大的Swap分區,但是我發現其實這個Swap幾乎很少用到,其實是浪費了磁盤空間。所以如果根據系統實際情況和內存的負荷綜合考慮,其實應該按照第二個參考標準設置為8G即可。當然這個只是個人的一些認知。

釋放Swap分區空間

[root@testlnx ~]# free -m
total used free shared buffers cached
Mem: 64556 55368 9188 0 926 51405
-/+ buffers/cache: 3036 61520
Swap: 65535 13 65522
[root@testlnx ~]# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 67108856 14204 -1

使用swapoff關閉交換分區

[root@testlnx ~]# swapoff /dev/mapper/VolGroup00-LogVol01

使用swapon啟用交換分區,此時查看交換分區的使用情況,你會發現used為0了

[root@testlnx ~]# swapon /dev/mapper/VolGroup00-LogVol01
[root@testlnx ~]# free -m
total used free shared buffers cached
Mem: 64556 55385 9171 0 926 51406
-/+ buffers/cache: 3052 61504
Swap: 65535 0 65535
[root@testlnx ~]#

Swap分區空間什麼時候使用

系統在什麼情況或條件下才會使用Swap分區的空間呢? 其實是Linux通過一個參數swappiness來控制的。當然還涉及到複雜的算法。

這個參數值可為 0-100,控制系統 swap 的使用程度。高數值可優先系統性能,在進程不活躍時主動將其轉換出物理內存。低數值可優先互動性並儘量避免將進程轉換處物理內存,並降低反應延遲。默認值為 60。注意:這個只是一個權值,不是一個百分比值,涉及到系統內核複雜的算法。關於該參數請參考這篇文章[轉載]調整虛擬內存,在此不做過多贅述。下面是關於swappiness的相關資料

The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."

Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.

有兩種臨時修改swappiness參數的方法,系統重啟後失效

方法1:
[root@DB-Server ~]# more /proc/sys/vm/swappiness
60
[root@DB-Server ~]# echo 10 > /proc/sys/vm/swappiness
[root@DB-Server ~]# more /proc/sys/vm/swappiness
10

方法2
[root@DB-Server ~]#sysctl vm.swappiness=10

永久修改swappiness參數的方法就是在配置文件/etc/sysctl.conf裡面修改vm.swappiness的值,然後重啟系統

echo 'vm.swappiness=10' >>/etc/sysctl.conf

如果有人會問是否物理內存使用到某個百分比後才會使用Swap交換空間,可以明確的告訴你不是這樣一個算法,如下截圖所示,及時物理內存只剩下8M了,但是依然沒有使用Swap交換空間,而另外一個例子,物理內存還剩下19G,居然用了一點點Swap交換空間。

Swap交換分區概念

Swap交換分區概念

另外調整/proc/sys/vm/swappiness這個參數,如果你沒有絕對把握,就不要隨便調整這個內核參數,這個參數符合大多數情況下的一個最優值。

Swap交換分區對性能的影響

我們知道Linux可以使用文件系統中的一個常規文件或獨立分區作為Swap交換空間,相對而言,交換分區要快一些。但是和RAM比較而言,Swap交換分區的性能依然比不上物理內存,目前的服務器上RAM基本上都相當充足,那麼是否可以考慮拋棄Swap交換分區,是否不需要保留Swap交換分區呢?這個其實是我的疑問之一。在這篇What Is a Linux SWAP Partition, And What Does It Do?博客中,作者給出了swap交換空間的優劣

Advantages:

  1. Provides overflow space when your memory fills up completely
  2. Can move rarely-needed items away from your high-speed memory
  3. Allows you to hibernate

Disadvantages:

  1. Takes up space on your hard drive as SWAP partitions do not resize dynamically
  2. Can increase wear and tear to your hard drive
  3. Does not necessarily improve performance (see below)

其實保留swap分區概括起來可以從下面來看:

首先,當物理內存不足以支撐系統和應用程序(進程)的運作時,這個Swap交換分區可以用作臨時存放使用率不高的內存分頁,把騰出的內存交給急需的應用程序(進程)使用。有點類似機房的UPS系統,雖然正常情況下不需要使用,但是異常情況下, Swap交換分區還是會發揮其關鍵作用。

其次,即使你的服務器擁有足夠多的物理內存,也有一些程序會在它們初始化時殘留的極少再用到的內存分頁內容轉移到 swap 空間,以此讓出物理內存空間。對於有發生內存洩漏幾率的應用程序(進程),Swap交換分區更是重要,因為誰也不想看到由於物理內存不足導致系統崩潰。

最後,現在很多個人用戶在使用Linux,有些甚至是PC的虛擬機上跑Linux系統,此時可能常用到休眠(Hibernate),這種情況下也是推薦劃分Swap交換分區的。

其實少量使用Swap交換空間是不會影響性能,只有當RAM資源出現瓶頸或者內存洩露,進程異常時導致頻繁、大量使用交換分區才會導致嚴重性能問題。另外使用Swap交換分區頻繁,還會引起kswapd0進程(虛擬內存管理中, 負責換頁的)耗用大量CPU資源,導致CPU飆升。

關於Swap分區的優劣以及是否應該捨棄,我有點惡趣味的想到了這個事情:人身上的兩個器官,闌尾和扁桃體。切除闌尾或扁桃體是否也是爭論不休。另外,其實不要Swap交換分區,Linux也是可以正常運行的(有人提及過這個問題)

調整Swap分區的大小

如下測試案例所示,Swap分區大小為65535M,我現在想將Swap分區調整為8G,那麼我們來看看具體操作吧

1:查看Swap的使用情況以及相關信息

[root@getlnx14uat ~]# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 67108856 878880 -1
[root@getlnx14uat ~]# free -m

total used free shared buffers cached
Mem: 3957 3920 36 0 39 3055
-/+ buffers/cache: 825 3132
Swap: 65535 858 64677

2: 關閉Swap交換分區

[root@getlnx14uat ~]# swapoff /dev/mapper/VolGroup00-LogVol01
[root@getlnx14uat ~]# swapon -s
Filename Type Size Used Priority

3: 這裡是縮小Swap分區大小,如果是增大Swap分區大小,那麼就需要擴展正在使用的swap分區的邏輯卷,此處使用lvreduce命令收縮邏輯卷。

[root@getlnx14uat ~]# lvreduce -L 8G /dev/mapper/VolGroup00-LogVol01
WARNING: Reducing active logical volume to 8.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
Reducing logical volume LogVol01 to 8.00 GB
Logical volume LogVol01 successfully resized

4:格式化swap分區

[root@getlnx14uat ~]# mkswap /dev/mapper/VolGroup00-LogVol01
Setting up swapspace version 1, size = 8589930 kB

5:啟動swap分區,並增加到/etc/fstab自動掛載

[root@getlnx14uat ~]# swapon -s
Filename Type Size Used Priority
[root@getlnx14uat ~]# swapon /dev/mapper/VolGroup00-LogVol01
[root@getlnx14uat ~]# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 8388600 0 -1
Swap交換分區概念

https://wiki.archlinux.org/index.php/swap

http://blog.csdn.net/tianlesoftware/article/details/8741873

http://www.makeuseof.com/tag/swap-partition/


分享到:


相關文章: