Linux Swap交換分區介紹總結

Swap交換分區概念 php

 

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

 

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分區大小 app

 

查看Swap分區的大小以及使用狀況,通常使用free命令便可,以下所示,Swap大小爲2015M,目前沒有使用Swap分區dom

[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,使用狀況等詳細信息ide

[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/

相關文章
相關標籤/搜索