Linux 釋放Cache

  有時候生產會發現,運行一段時間swap就會飆升,而且不下來,其實內存這個東西,怎麼說夠用不夠用呢,看下swap就行,如果這個swap長時間動態平衡之後又增加,那麼估計是有問題。

Linux 釋放Cache
有關/proc/sys/vm/drop_caches的用法在下面進行了說明
 
/proc/sys/vm/drop_caches (since Linux 2.6.16)
 
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become free.
 
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 > /proc/sys/vm/drop_caches.

 
Because this is a non-destructive operation and dirty objects



這個定時清理內存的一加,有問題你的開發也發現不了 盒盒~~
當發生內存不足、應用獲取不到可用內存、OOM錯誤等問題時,還是更應該去分析應用方面的原因,如用戶量太大導致內存不足、發生應用內存溢出等情況,否則,清空buffer,強制騰出free的大小,可能只是把問題給暫時屏蔽了

想想一個情況,當你已經預定腳本在每天下午2點來清除內存緩存。那麼其時該腳本會執行並刷新你的內存緩存。在某一天由於某些原因,可能您的網站的在線用戶會超過預期地從你的服務器請求資源。
而在這時,按計劃調度的腳本運行了,並清除了緩存中的一切。當所有的用戶都從磁盤讀取數據時,這將導致服務器崩潰並損壞數據庫。因此,清除緩存僅在必要時並且在你的預料之中

#!/bin/bash

used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
echo "===========================" >> /var/log/mem.log
date >> /var/log/mem.log
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log
if [ $free -le 100 ] ; then
                sync && echo 1 > /proc/sys/vm/drop_caches
                sync && echo 2 > /proc/sys/vm/drop_caches
                sync && echo 3 > /proc/sys/vm/drop_caches
                echo "OK" >> /var/log/mem.log
else
                echo "Not required" >> /var/log/mem.log
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章