Linux Swap交換分割槽介紹總結

dbasdk發表於2018-01-12

Linux Swap交換分割槽介紹總結











Linux Swap交換分割槽介紹總結

2016-03-06 00:22 by 瀟湘隱者, 29747 閱讀, 2 評論, 收藏編輯

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 ~]# 

clip_image001

 

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交換空間。

clip_image002

clip_image003

另外調整/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

clip_image004

 

參考資料:

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

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

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

 








About Me

.............................................................................................................................................

● 本文作者:小麥苗,部分內容整理自網路,若有侵權請聯絡小麥苗刪除

● 本文在itpub(http://blog.itpub.net/26736162/abstract/1/)、部落格園(http://www.cnblogs.com/lhrbest)和個人微信公眾號(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文部落格園地址:http://www.cnblogs.com/lhrbest

● 本文pdf版、個人簡介及小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址:http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

.............................................................................................................................................

● QQ群號:230161599(滿)、618766405

● 微信群:可加我微信,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友646634621,註明新增緣由

● 於 2018-01-01 06:00 ~ 2018-01-31 24:00 在魔都完成

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

.............................................................................................................................................

小麥苗的微店https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書http://blog.itpub.net/26736162/viewspace-2142121/

好訊息:小麥苗OCP、OCM開班啦,詳情請點選http://blog.itpub.net/26736162/viewspace-2148098/

.............................................................................................................................................

使用微信客戶端掃描下面的二維碼來關注小麥苗的微信公眾號(xiaomaimiaolhr)及QQ群(DBA寶典),學習最實用的資料庫技術。

   小麥苗的微信公眾號      小麥苗的DBA寶典QQ群2     《DBA筆試面寶典》讀者群       小麥苗的微店

.............................................................................................................................................

Linux Swap交換分割槽介紹總結
DBA筆試面試講解群
《DBA寶典》讀者群 歡迎與我聯絡



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2149754/,如需轉載,請註明出處,否則將追究法律責任。

相關文章