linux下釋放cache內存

細心的朋友會注意到,當你在linux下頻繁存取文件後,物理內存會很快被用光,當程序結束後,內存不會被正常釋放,而是一直做爲caching.這個問題,貌似有很多人在問,不過都沒有看到有什麼很好解決的辦法.那麼我來談談這個問題.html

  先來講說free命令node

  [root@server ~]# free -mlinux

  total       used       free     shared    buffers     cached緩存

  Mem:           249        163         86          0         10         94ide

  -/+ buffers/cache:         58        191this

  Swap:          511          0        511url

  其中:設計

  total 內存總數server

  used 已經使用的內存數htm

  free 空閒的內存數

  shared 多個進程共享的內存總額

  buffers Buffer Cache和cached Page Cache 磁盤緩存的大小

  -buffers/cache 的內存數:used - buffers - cached

  +buffers/cache 的內存數:free + buffers + cached

  可用的memory=free memory+buffers+cached

  有了這個基礎後,能夠得知,我如今used爲163MB,free爲86,buffer和cached分別爲10,94

  那麼咱們來看看,若是我執行復制文件,內存會發生什麼變化.

  [root@server ~]# cp -r /etc ~/test/

  [root@server ~]# free -m

  total       used       free     shared    buffers     cached

  Mem:           249        244          4          0          8        174

  -/+ buffers/cache:         62        187

  Swap:          511          0        511

  在我命令執行結束後,used爲244MB,free爲4MB,buffers爲8MB,cached爲174MB,天吶都被cached吃掉了.別緊張,這是爲了提升文件讀取效率的作法.

  引用[url]http://www.2qyou.com/thread-591-1-1.html[/url] 爲了提升磁盤存取效率, Linux作了一些精心的設計, 除了對dentry進行緩存(用於VFS,加速文件路徑名到inode的轉換), 還採起了兩種主要Cache方式:Buffer Cache和Page Cache。前者針對磁盤塊的讀寫,後者針對文件inode的讀寫。這些Cache有效縮短了 I/O系統調用(好比read,write,getdents)的時間。"

  那麼有人說過段時間,linux會自動釋放掉所用的內存,咱們使用free再來試試,看看是否有釋放>?

  [root@server test]# free -m

  total       used       free     shared    buffers     cached

  Mem:           249        244          5          0          8        174

  -/+ buffers/cache:         61        188

  Swap:          511          0        511

  MS沒有任何變化,那麼我可否手動釋放掉這些內存呢???回答是能夠的!

  /proc是一個虛擬文件系統,咱們能夠經過對它的讀寫操做作爲與kernel實體間進行通訊的一種手段.也就是說能夠經過修改/proc中的文件,來對當前kernel的行爲作出調整.那麼咱們能夠經過調整/proc/sys/vm/drop_caches來釋放內存.操做以下:

  [root@server test]# cat /proc/sys/vm/drop_caches

  0

  首先,/proc/sys/vm/drop_caches的值,默認爲0

  [root@server test]# sync

  手動執行sync命令(描述:sync 命令運行 sync 子例程。若是必須中止系統,則運行 sync 命令以確保文件系統的完整性。sync 命令將全部未寫的系統緩衝區寫到磁盤中,包含已修改的 i-node、已延遲的塊 I/O 和讀寫映射文件)

  [root@server test]# echo 3 > /proc/sys/vm/drop_caches

  [root@server test]# cat /proc/sys/vm/drop_caches

  3

  將/proc/sys/vm/drop_caches值設爲3

  [root@server test]# free -m

  total       used       free     shared    buffers     cached

  Mem:           249         66        182          0          0         11

  -/+ buffers/cache:         55        194

  Swap:          511          0        511

  再來運行free命令,發現如今的used爲66MB,free爲182MB,buffers爲0MB,cached爲11MB.那麼有效的釋放了buffer和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:

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

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

  #echo  3  >/proc/sys/vm/drop_caches.

  Because this is a non-destructive operation  and  dirty  objects

補充:top命令按住M,是關於內存的從大到小的排序,按住P是關於CPU的佔用率排序,  pmap 進程號顯示進程佔用的內存,PS  X |GREP 進程號也有此做用。vmstat顯示系統資源、內存的佔用率。

相關文章
相關標籤/搜索