天天看點

Linux釋放cache記憶體Linux釋放cache記憶體

/proc是個虛拟文檔系統,我們能夠通過對他的讀寫操作做為和kernel實體間進行通信的一種手段。也就是說能夠通過修改/proc中的文檔,來對目前kernel的行為做出調整。

Linux釋放cache記憶體Linux釋放cache記憶體

那麼我們能夠通過調整/proc/sys/vm/drop_caches來釋放記憶體。操作如下:

1、檢視 /proc/sys/vm/drop_caches的值,預設為0:

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

2、手動執行sync指令:

[root@server test]# sync

sync 指令運作 sync 子例程。假如必須停止系統,則運作 sync 指令以確定文檔系統的完整性。sync 指令将任何未寫的系統緩沖區寫到磁盤中,包含已修改的 i-node、已延遲的塊 i/o 和讀寫映射文檔。

3、将/proc/sys/vm/drop_caches值設為3

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

3

4、再來運作free指令:

[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

發現現在的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, 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 are not freeable, the user should run sync(8) first.

原文釋出時間:2014-09-11

本文來自雲栖合作夥伴“linux中國”

繼續閱讀