/proc是个虚拟文档系统,我们能够通过对他的读写操作做为和kernel实体间进行通信的一种手段.也就是说能够通过修改/proc中的文档,来对当前kernel的行为做出调整.那么我们能够通过调整/proc/sys/vm/drop_caches来释放内存.操作如下:

[root@servertest]#cat/proc/sys/vm/drop_caches

0

首先,/proc/sys/vm/drop_caches的值,默认为0

[root@servertest]#sync

手动执行sync命令(描述:sync命令运行sync子例程。假如必须停止系统,则运行sync命令以确保文档系统的完整性。sync命令将任何未写的系统缓冲区写到磁盘中,包含已修改的i-node、已延迟的块I/O和读写映射文档)

[root@servertest]#echo3>/proc/sys/vm/drop_caches

[root@servertest]#cat/proc/sys/vm/drop_caches

3

将/proc/sys/vm/drop_caches值设为3

[root@servertest]#free-m

totalusedfreesharedbufferscached

Mem:249661820011

-/+buffers/cache:55194

Swap:5110511

再来运行free命令,发现现在的used为66MB,free为182MB,buffers为0MB,cached为11MB.那么有效的释放了buffer和cache.

有关/proc/sys/vm/drop_caches的用法在下面进行了说明

/proc/sys/vm/drop_caches(sinceLinux2.6.16)

Writingtothisfilecausesthekerneltodropcleancaches,

dentriesandinodesfrommemory,causingthatmemorytobecome

free.

Tofreepagecache,useecho1>/proc/sys/vm/drop_caches;to

freedentriesandinodes,useecho2>/proc/sys/vm/drop_caches;

tofreepagecache,dentriesandinodes,useecho3>

/proc/sys/vm/drop_caches.

Becausethisisanon-destructiveoperationanddirtyobjects

arenotfreeable,theusershouldrunsync(8)first.