CentOS 释放内存

内存占用原因
当读写文件的时候,Linux内核为了提高读写性能与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory.
即使你的程序运行结束后,Cache Memory也不会自动释放.这就会导致你在Linux系统中程序频繁读写文件后,你会发现可用物理内存会很少.

 

处理方法一
Cache Memory在你需要使用内存的时候会自动释放,所以你不必担心没有内存可用.

处理方法二
如果你希望手动去释放Cache Memory也是有办法的:
使用 sync;sync;echo 1 > /proc/sys/vm/drop_caches 命令.

效果很显著.


命令解释
sync命令的作用是将有关文件系统的存储器常驻信息送入物理介质内.
为确保可靠起见,应执行两遍sync命令,这是因为sync命令完成时,并不保证信息实际写到了磁盘上.

 

清空 pagecache
echo 1 > /proc/sys/vm/drop_caches
sysctl -w vm.drop_caches=1命令与上述命令效果相同

 

清空所有缓存(pagecache、dentries 和 inodes)
echo 3 > /proc/sys/vm/drop_caches
sysctl -w vm.drop_caches=3命令与上述命令效果相同

 

关于drop_caches的官方说法
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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值