Linux手动清除内存缓存的方法 释放内存
Q:手动释放Linux内存A:清除系统对内存的cache,使用root做下面几步:
注意运行所有命令前要指向sync来同步数据,否则会有部分数据丢失的情况发生!
sync
echo 3 > /proc/sys/vm/drop_caches
sync
echo 0 > /proc/sys/vm/drop_caches
注解:/proc是一个虚拟文件系统,我们可以通过对它的读写操作做为与kernel实体间进行通信的一种手段。也就是说可以通过修改 /proc中的文件,来对当前kernel的行为做出调整。那么我们就可以通过调整/proc/sys/vm/drop_caches来释放内存。
手动执行sync命令是为了确保文件系统的完整性(描述:sync命令运行sync 子例程。如果必须停止系统,则运行sync命令以确保文件系统的完整性。sync命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件),所以这一步必须先做。
有关/proc/sys/vm/drop_caches的用法如下:
Writing to this will cause 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
##To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
##To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
其他文章参考:
有一个站是采集的
我们知道,采集基本上是半小时就操作下,这样下来很费内存
我一天下来就吃入1G左右的内存,太蛋疼了!
今天终于找到方法,发现造成采集内存增加主要是:
pagecache,也就是页面cache
那怎么处理呢,找了一个英文的
Kernels 2.6.16 and newer provide a mechanism to have the kernel drop the page cache and/or inode and dentry caches on command, which can help free up a lot of memory. Now you can throw away that script that allocated a ton of memory just to get rid of the cache...
To use /proc/sys/vm/drop_caches , just echo a number to it.
To free pagecache:
# echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
# echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run "sync" first to flush them out to disk, these drop operations will tend to free more memory.
原来900多M的内存,一下子就到100M了,哈哈!
不错啊!
来自坚持吧 Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches
频繁的文件访问会导致系统的Cache使用量大增
$ free -m
total used free shared buffers cached
Mem: 3955 3926 28 0 55 3459
-/+ buffers/cache: 411 3544
Swap: 5726 0 5726
free内存减少到几十兆,系统运行缓慢
运行sync将dirty的内容写回硬盘
$sync
通过修改proc系统的drop_caches清理free的cache
$echo 3 > /proc/sys/vm/drop_caches
drop_caches的详细文档如下:
Writing to this will cause 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
To free dentries and inodes:
* echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
* echo 3 > /proc/sys/vm/drop_caches
As this is a non-destructive operation, and dirty objects are notfreeable, the user should run "sync" first in order to make sure allcached objects are freed.
This tunable was added in 2.6.16.
修改/etc/sysctl.conf 添加如下选项后就不会内存持续增加
vm.dirty_ratio = 1
vm.dirty_background_ratio=1
vm.dirty_writeback_centisecs=2
vm.dirty_expire_centisecs=3
vm.drop_caches=3
vm.swappiness =100
vm.vfs_cache_pressure=163
vm.overcommit_memory=2
vm.lowmem_reserve_ratio=32 32 8
kern.maxvnodes=3
上面的设置比较粗暴,使cache的作用基本无法发挥。需要根据机器的状况进行适当的调节寻找最佳的折衷。
页:
[1]