RHCA RH442實戰系列(二)之Linux buffer內存釋放

1. 說明node

  Linux的內核負責硬件管理,資源調度,進程管理,和資源管理等相關工做,其中內存資源管理作爲kernel的一項很是重要的工做。kernel在處理文件時,如打開一個文件,會將文件的元數據信息,即文件名,inode等信息記錄在buffer中,後續重複讀取相同的文件,則直接衝buffer中讀取,這樣的機制可以提升速度,此外,對於文件的內容,將會記錄在cache中保存,對於buffer和cache,內存會有自動清理的機制,若是buffer和cache一直沒法釋放,可能致使的緣由有:內存泄露,應用程序有問題等緣由。
app

2. 現象說明ide

  生產環境中,使用了兩臺大硬盤的機器作glusterfs集羣,用於openstack的cinder作volume卷的角色,隨着時間的推移,發現兩臺機器的內存利用率超過了95%,上機器上查看時,發現buffer和cache的利用率很是高,以下:spa

[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         32057      30861       1195          0          6         29
-/+ buffers/cache:          30825       1232            #buffer已經使用了接近30G,只剩下1G左右的空間
Swap:         8191          0       8191

3. 解決方法orm

  1. 針對buffer和cache太高,只用sync將數據回寫到磁盤blog

[root@localhost ~]# sync && sync && sync
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         32057      30854       1203          0          7         29
-/+ buffers/cache:          30817       1240            #相比於上次的結果,釋放了100M的內存,沒有明顯的效果
Swap:         8191          0       8191

2. 修改內存對swap的親和力,轉移至swap中進程

[root@localhost ~]# sysctl -a |grep swap
vm.swappiness = 60                                            #kernel對於swap的親和力爲60,設置爲0,則表示直接使用swap空間

#調整swap的親和力
[root@localhost ~]# sysctl -w vm.swappiness=0
vm.swappiness = 0

[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         32057      30857       1200          0          7         29
-/+ buffers/cache:      30820       1237                      #依舊沒有什麼明顯的效果
Swap:         8191          0       8191

#調整回來
[root@localhost ~]# sysctl -w vm.swappiness=60
vm.swappiness = 60

3. 經過以上的兩種方法嘗試,都沒有達到釋放內存的效果,上面的方法,都是比較保守可用的方法,如下經過修改內核參數的方式釋放內存圖片

[root@localhost ~]# sysctl -a |grep drop_caches
vm.drop_caches = 0                                            #默認爲0,表示默認的機制

#修改成1,釋放pagecache,執行前,執行屢次sync
[root@localhost ~]# sync && sync && sync && sysctl -w vm.drop_caches=1
vm.drop_caches = 1
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         32057      30836       1221          0          0         14
-/+ buffers/cache:      30821       1236                       #依舊沒有明顯的內存資源釋放
Swap:         8191          0       8191

#修改成3,釋放 pagecache, dentries and inodes
[root@localhost ~]# sync && sync && sync && sysctl -w vm.drop_caches=3    

[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         32057       1715      30342          0         12         34
-/+ buffers/cache:       1667      30389                      #效果立竿見影!!!!,直接釋放了30G的內存
Swap:         8188          0       8188

#修改回原始值,千萬記得!!
[root@localhost ~]# sysctl -w vm.drop_caches=0

4. 觀察監控內存的使用狀況ip

wKioL1ai7tPCPg0-AABP2AS_nLA577.png


4. 參數說明內存

Drop Caches

Kernels 2.6.16 and newer provide a mechanism to have the kernel drop the page cache and/or inode and dentry caches on command, which can help free up a lot of memory. Now you can throw away that script that allocated a ton of memory just to get rid of the cache...

To use /proc/sys/vm/drop_caches, just echo a number to it.

To free pagecache:

# echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

# echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run "sync" first to flush them out to disk, these drop operations will tend to free more memory.


5. 結論

  關於內存的釋放,以上經過暴力的方式,直接釋放了保存在內存中的inode和pagecache,關因而否形成數據丟失,還在進步一觀察中,若是有發現相似的情況,建議重啓進程,或檢查應用程序是否有內存泄露等問題,至因而否可以執行,請讀者謹慎,僅做參考。

相關文章
相關標籤/搜索