1、查看Linux系統當前的分區狀況:瀏覽器
>free -mcookie
簡單來講,SWAP 交換分區是用來存放內存溢出來的數據。咱們能夠將內存想像成一個盆子,運行程序時的數據都被做爲水放在內存這個盆子當中。若是你運行太多的應用程序,致使水過多而(內存這個)盆子已經裝不下時,就會將多於部分的水裝到 SWAP 交換分區當中。app
從理論上講,SWAP 交換分區增大了可使用的內存空間,但事實並不是如此。由於內存的數據交換速度是很是快的,而 SWAP 交換分區是把物理磁盤當成內存的載體,也就是說:雖然使用 SWAP 可讓咱們運行更多或更大的應用程序了,但物理磁盤的數據交換速度遠比不上物理內存,所以也就可能形成運行性能的降低。而對硬盤特別是 SSD 磁盤的頻繁讀寫,也會形成磁盤壽命的縮短和性能降低。tcp
其實,咱們能夠把 SWAP 交換分區比喻成 Windows 中的 pagefile 頁面文件,但二者在技術實現上仍有許多差別。性能
SWAP優先級spa
SWAP 交換分區會將內存中較少使用的數據移到磁盤當中,從而將更多的內存空間騰出來給其它的重要應用,也就是說:內存中較少使用的數據會被移動 SWAP 分區。debug
「較少使用」其實取決於「swappiness」閾值,在 Linux 中實際上是一個可配置項。較高在 swappiness 值意味着數據更有可能被移到 SWAP 分區,較低的 swappiness 表示數據不太可能被移到 SWAP 分區。ip
SWAP與Linux休眠內存
最後還有一點須要注意:Linux 系統休眠的數據是存儲到 SWAP 交換分區中的,也就是說若是系統沒有配置 SWAP 交換分區,在 Linux 上是沒法進行休眠的。it
你是否須要SWAP分區呢?
你有必要爲本身的 Linux 劃一個 SWAP 分區嗎?SWAP 交換分區對你來講是必要的嗎?
絕對不是! Linux 系統不配置 SWAP 也能夠運行得很好,是否須要劃分 SWAP 分區也有以下幾個優缺點:
優勢:內存用完時,提供更多運行空間
能夠休眠
缺點:SWAP對磁盤的佔用設定好以後是固定的,沒法動態調整
增長了磁盤的讀寫次數和損耗概率
不必定提升性能(詳細解釋見下文)
SWAP起副作用的狀況
開啓 SWAP 交換分區也並不是問題能提高 Linux 性能,在某些狀況下使用 SWAP 可能比不使用更糟。
若是你在一臺1GB內存和5400轉硬盤的上網本上安裝了 Linux,只須要多打開幾個瀏覽器標籤並多開幾個應用就會形成內存佔完而開始使用 SWAP 交換分區。因爲硬盤只有5400轉,隨後便開始出現性能瓶頸。這是由於硬盤轉速太慢,而又有大量的數據須要訪問到 SWAP 交換分區,因此會形成機器愈來愈慢。在此時,咱們一般須要重啓系統來徹底清除 SWAP 的內容。
關於SWAP的建議
若是你須要 Linux 休眠功能,那麼固然就要劃分 SWAP 分區。該分區通常爲你內存大小外加 10-25%
若是你的內存小於4GB,而且但願獲得小的性能提高(至少7200轉磁盤),能夠劃分一個交換分區
若是你的磁盤轉速小於7200轉,建議不要劃分 SWAP 交換分區
更改Swappiness
咱們能夠直接在 /etc/sysctl.conf 文件中對 swappiness 值進行編輯,在該文件中找到 vm.swappiness 並對其值進行更改(最好是10),若是你沒找到這個參數,能夠直接在文件末尾添加:
# vm.swappiness=10
該值主要表示咱們但願 Linux 從內存與 SWAP 分區進行數據交換的強度,值爲 10 則表示當內存佔用到 90% 時開始將數據寫入 SWAP。Ubuntu 的默認 swappiness 值爲 60,這表示當內存佔用到 40% 時開始將數據寫入 SWAP。
[root@AY140716161543837722Z etc]# cat sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
# Disable IPv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# Determines how often to check for stale neighbor entries.
net.ipv4.neigh.default.gc_stale_time=120
# Using arp_announce/arp_ignore to solve the ARP Problem
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce=2
vm.swappiness = 0
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.conf.lo.arp_announce=2
[root@AY140716161543837722Z etc]#