歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux swap交換空間

linux swap交換空間

日期:2017/3/1 12:23:30   编辑:關於Linux
linux內存通過 virtual memory 虛擬內存來管理整個內存, physical RAM物理內存和swap交換空間即為virtual memory總量。 swap的使用場景 swap主要有兩個用處 1、當系統需要比物理內存更多的內存空間的時候,內核會把內存裡邊用得比較少的內存頁面swap out到交換分區,以空出物理內存給當前應用來快速運行。 2、某些應用啟動的時候初始化但是隨後的應用運行期間不再使用的內存頁面,系統會把這部分頁面也swap out到交換空間,以留出物理內存頁面給其他應用或者磁盤緩存。 linux的這種內存管理策略主要用來節約物理內存,提升當前應用的執行速度。但是swap不能當做擴充內存的一個手段,因為swap讀寫屬於磁盤io,要比物理內存的io慢得多。 如果系統頻繁的swap out內存頁面到交換分區,隨後又swap in交換分區到內存頁面,這說明系統在尋找空閒內存來是多個應用同時運行,也就是說當前系統任務比較繁忙,但是可用內存又不足了,這時候唯一的辦法只能通過提高物理內存來解決。 因此衡量一個系統內存用量是否到了瓶頸,就可以通過觀察swap用量和si so的頻率來評估。 生成swap swap空間有兩種形式:一是交換分區,二是交換文件。總之對它的讀寫都是磁盤操作。 1、交換分區 交換分區可以在安裝操作系統的時候分配,也可以進入系統後用fdisk來劃分一個交換分區 比如在磁盤上劃分一個/dev/sda5分區,然後標記為交換分區。 然後使用mkswap命令在該分區上面建立交換分區文件系統: mkswap /dev/sda5 最後激活交換分區: swapon /dev/sda5 2、交換文件 交換空間也可以是文件,只需要用dd命令在磁盤上建立分配了大小的文件/home/swapfile 建立交換分區文件系統 mkswap /home/swapfile 最後激活它 swapon /home/swapfile swap空間大小的分配策略 swap空間大小的分配眾說紛纭,有說物理內存兩倍的,有說物理內存一樣大小的,也有說物理內存一半的。在不同場景下,這些說法應該都是對的。(因此拿一個絕對說法當面試題的,考官可能也不真正的懂swap) 下面是紅帽系的分配策略: Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB. So, if: M = Amount of RAM in GB, and S = Amount of swap in GB, then If M < 2 S = M *2 Else S = M + 2 居然有個if else的偽代碼來計算swap,最後還有一句: For systems with really large amounts of RAM (more than 32 GB) you can likely get away with a smaller swap partition (around 1x, or less, of physical RAM). 所以怎麼分,還得看具體應用場景,只要不離譜,都是正確的。 紅帽系還有個注意項: Important File systems and LVM2 volumes assigned as swap space cannot be in use when being modified. For example, no system processes can be assigned the swap space, as well as no amount of swap should be allocated and used by the kernel. Use the free and cat /proc/swaps commands to verify how much and where swap is in use. The best way to achieve swap space modifications is to boot your system in rescue mode, and then follow the instructions (for each scenario) in the remainder of this chapter. Refer to the Red Hat Enterprise Linux Installation Guide for instructions on booting into rescue mode. When prompted to mount the file system, select Skip. 下面是另一個swap分配策略,適合大部分系統和場景: A rule of thumb is as follows: 1) for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications; 2) for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary; 3) for older desktop machines (with say only 128MB), use as much swap space as you can spare, even up to 1GB. swap優化 linux內2.6增加一個新參數來管理swap,叫做swappiness ,Swappiness 可以有 0 到 100 的值。設置這個參數為較低的值會減少內存的交換,從而提升一些系統上的響應度。 值越高,內存頁面越多的swap out到交換空間,值越低,越多的應用使用物理內存空間。因此要想最大限度的使用物理內存,應該盡量減小swappiness的值甚至設置為0。 linux默認設置為60,臨時修改它的值: echo 10 > /proc/sys/vm/swappiness 要永久修改可以在/etc/sysctl.conf文件修改vm.swappiness參數
Copyright © Linux教程網 All Rights Reserved