一文說盡 Linux 系統的 swap 交換空間

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你好,我是看山。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用 Ubuntu 已經將近 1 年了,最近重裝了 16.04 之後,每天到下午 5 點左右,都會發現 Swap 交換空間有幾百兆的寫入,系統內存 8G,硬盤是 SSD,i5 處理器,配置中檔,也沒有啓動什麼大型軟件,就是用 IDEA 做開發,雖然沒有影響,但本着一顆求知的心,google 一下,第一篇是 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.linux.com/news/all-about-linux-swap-space","title":"","type":null},"content":[{"type":"text","text":"《All about Linux swap space》","attrs":{}}]},{"type":"text","text":",口氣很大,直接翻譯了。(譯文是在 2016 年 5 月寫的,雖然已經是 5 年前,但是 Linux 架構並沒有發生變化,所以本文所述還是正確的。)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3e/3ee95997d271adfa959fe4dc238e536a.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 將隨機存儲 RAM 稱爲內存頁。交換技術就是將一頁內存複製到預先設定的硬盤上的交換空間,來釋放該頁佔用內存。物理內存和交換空間的和就是可提供的虛擬內存的總量。有兩個原因證明交換技術是很重要的。首先,系統需要的內存量比物理內存更大時,系統內核可以把較少使用的內存頁寫到交換空間,把空閒出來的內存給當前的應用程序(進程)使用。其次,一個應用啓動時使用的內存頁,可能只是在初始化時使用,之後不會再用,操作系統就可以把這部分內存頁寫入交換空間,把空閒出來的內存給其他應用使用或作爲磁盤高速緩存。但是,交換技術也有負面作用。相對於內存,硬盤讀寫速度慢。內存的讀寫速度可以使用納秒衡量,但是硬盤的速度只能達到毫秒級,訪問硬盤的速度比訪問內存的速度慢成千上萬倍。發生的交換越多,系統運行越慢。有時候會有過度的交換或內存頁頻繁的寫入寫出的抖動發生,因爲系統既要保證應用正常運行,又要尋找空閒的內存。這種情況下,只能通過增加 RAM 來解決。Linux 有兩種形式的交換空間:交換分區和交換文件。交換分區就是一個獨立的硬盤,沒有文件或內容。交換文件是文件系統中的一個特殊文件,獨立於系統和數據文件之外。可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -s","attrs":{}}],"attrs":{}},{"type":"text","text":"命令查看 swap 空間,輸出如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"Filename Type Size Used Priority\n/dev/sda5 partition 859436 0 -1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每一行列出的都是系統正在使用的交換空間。這裏的'Type'字段表明該交換空間是一個分區而不是文件,通過'Filename'可以知道交換分區是磁盤 sda5。'Size'字段磁盤大小,單位是 KB,'Used'字段是表示有多少交換空間被使用。'Priority'字段表示 Linux 系統的交換空間使用優先級。有一個重要的特性,如果在 Linux 系統中掛載兩個(或更多)具有相同優先級的交換空間(最好是兩個不同的設備),Linux 將交替使用,可以提升交換性能。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"交換分區","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要爲系統添加一個額外的交換分區,首先你需要準備一個。第一步是確保分區標記爲交換分區,第二步是將格式設置爲 swap 文件系統。將分區標記爲 swap 分區,以 root 權限運行:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"fdisk -l /dev/hdb\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將'/dev/hdb'替換爲你的交換分區的磁盤。輸出類似於:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"Device Boot Start End Blocks Id System\n/dev/hdb1 2328 2434 859446 82 Linux swap / Solaris\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果分區沒有標記爲 swap 分區,你需要使用命令","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"fdisk","attrs":{}}],"attrs":{}},{"type":"text","text":"及參數 t 來聲明。操作分區時要小心,你絕對不想刪除重要的分區或把系統分區的標識改錯。交換分區上的數據會丟失,所以每次改動都需要多次確認。還需要注意的是,Solaris 使用相同的 ID 作爲 Linux 交換空間,所以需要小心不要殺掉 Solaris 分區。如果分區已經標記爲 swap 分區,就需要通過 root 權限運行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mkswap","attrs":{}}],"attrs":{}},{"type":"text","text":"命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"mkswap /dev/hdb1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果運行沒有錯誤,你的交換空間就開始使用。立即激活:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"swapon /dev/hdb1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -s","attrs":{}}],"attrs":{}},{"type":"text","text":"來確認是否運行。爲了在系統啓動時自動掛載 swap 空間,需要在'/etc/fstab'文件中添加一些列的配置,swap 空間是特殊的文件系統,許多參數不可用。比如:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"/dev/hdb1 none swap sw 0 0\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"檢查你的交換空間是無需重新啓動,你可以運行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapoff -a","attrs":{}}],"attrs":{}},{"type":"text","text":"命令,然後運行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -a","attrs":{}}],"attrs":{}},{"type":"text","text":",再通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -s","attrs":{}}],"attrs":{}},{"type":"text","text":"檢查。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"交換文件","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"和交換分區類似,Linux 也支持使用交換文件,你可以創建、準備,以交換分區的方式掛載。交換文件的好處是,你不需要找一個空的分區或添加額外的交換分區磁盤。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dd","attrs":{}}],"attrs":{}},{"type":"text","text":"命令創建一個空文件。創建一個 1G 的文件,比如:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"dd if=/dev/zero of=/swapfile bs=1024 count=1048576\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"'/swapfile'是交換文件的名字,'count'的 1048576 是文件大小,單位 KB。準備交換文件使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mkswap","attrs":{}}],"attrs":{}},{"type":"text","text":"命令,類似於準備分區,不過這次是使用同一個交換文件:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"mkswap /swapfile\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同樣的,掛載交換文件使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon","attrs":{}}],"attrs":{}},{"type":"text","text":"命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"swapon /swapfile\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在'/etc/fstab'中輸入下面的內容:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"/swapfile none swap sw 0 0\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"交換空間的大小","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你有很大的內存,有可能沒有交換空間,系統也能運行良好。但是如果物理內存耗光,系統就會崩潰,因爲它沒有其他緩解方式,所以最好還是提供一個交換空間,更何況磁盤比內存便宜很多。關鍵的問題是內存空間多大?老版的類 UNIX 操作系統要求交換空間是物理內存的兩到三倍。現在的擴展版(比如 Linux)不需要這麼多,但是如果你配置這些,他們也會使用。重要的原則如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"對於桌面系統,使用系統內存的兩倍的交換空間,將可以運行大量的應用程序(其中可能有很多閒置的),使更多的 RAM 用於主要的應用;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"對於服務器,使用小量的交換空間(通常是物理內存的一半),這樣你就可以通過監控交換空間的大小來預警是否需要增加 RAM;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"對於老式臺式機,使用盡可能大的交換空間","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 2.6 內核中增加一個新的內核參數'swappiness',管理員可以通過該參數修改 Linux 交換方式。參數值從 0 到 100. 從本質上說,值越大,將引起越多內存頁發生交換;值越小,就有越多的應用駐留在內存中,而交換空間是空閒的。內核維護者 Andrew Morton 說過,他在他的臺式機中設置 swappiness 值是 100,說:“我的觀點是,通過內核參數降低交換是錯誤的。你不需要幾百兆的無用應用佔用內存。把它放在磁盤上,把內存留給有用的東西。”Morton 的想法有一個漏洞,如果內存交換太快,應用響應就會下降,因爲當應用窗口被點擊時,應用正在從交換空間讀入內存,就會感覺運行很慢。默認的'swappiness'值是 60。你可以使用 root 命令調整參數(作用到重啓):","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"echo 50 > /proc/sys/vm/swappiness\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你需要使參數永久有效,就需要修改'/etc/sysctl.conf'中的'vm.swappiness'參數。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"結論","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"管理交換空間是系統管理的一個重要方面。有了良好的規劃和合理的使用交換技術可以有很多好處。不要害怕實驗,並且經常監控你的系統,以確保你得到你需要的結果。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"寫在最後","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就目前來說,內存和 SSD 都開始降價,基本上很輕鬆就能把機器攢到 8G(RAM)+120G(SSD),這樣的話,就個人用戶的桌面系統而言,交換空間的作用被大大削弱,但是正如上面說的,如果沒有交換空間,內存耗光的時候,機器就掛了。因爲 SSD 不建議分多個分區,所以使用 swap file 的方式比較好,而且還可以多建幾個 swap file 文件,提升交換性能。","attrs":{}}]},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你好,我是看山,10 年老猿,開源貢獻者。遊於碼界,戲享人生。關注公衆號:","attrs":{}},{"type":"link","attrs":{"href":"http://static.howardliu.cn/about/kanshanshuo.png","title":"","type":null},"content":[{"type":"text","text":"看山的小屋","attrs":{}}]},{"type":"text","text":",領取資料。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章