天天看點

手動釋放linux伺服器記憶體

這位朋友寫的很詳細

fengwei@linuxserver ~]$ free -m

             total       used       free     shared    buffers     cached

Mem:        15656      12163       3493         0         19      10136

-/+ buffers/cache:       2008      13648

Swap:        18047        181      17866

[fengwei@linuxserver ~SiRNA]$ su root

paasword:

[root@linuxserver ~]# cat /proc/sys/vm/drop_caches

[root@linuxserver ~]# sync

[root@linuxserver ~]# echo 1 >/proc/sys/vm/drop_caches

[root@linuxserver ~]# free -m

Mem:        15656       2016    13640         0          1        245

-/+ buffers/cache:       1768      13888

手動釋放緩存

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

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

首先,/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, use echo 1 > /proc/sys/vm/drop_caches;

tofree 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 first.

本文轉自aaa超超aaa 51CTO部落格,原文連結:http://blog.51cto.com/10983441/1762951

繼續閱讀