歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下Cache內存釋放

Linux下Cache內存釋放

日期:2017/2/28 16:00:40   编辑:Linux教程

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

[[email protected] test]# cat /proc/sys/vm/drop_caches

0

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

[[email protected] test]# sync

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

[[email protected] test]# echo 3 > /proc/sys/vm/drop_caches

[[email protected] test]# cat /proc/sys/vm/drop_caches

3

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

[[email protected] 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, 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.

Copyright © Linux教程網 All Rights Reserved