• linux清除cache的方法


    1  Linux下内存占用多的原因

    当linux第一次读取一个文件运行时,一份放到一片内存中cache起来,另一份放入运行程序的内存中,正常运行,当程序运行完,关闭了,cache中的那一分却没有释放,第二次运行的时候,系统先看看在内存中是否有一地次运行时存起来的cache中的副本,如果有的话,直接从内存中读取,那样,速度就快多了。

    说明这种情况的很典型的例子是启动firefox,由于firefox程序很大,因此第一次读取运行的时候很慢,尤其在速度不快的机器上,但是当你彻底关闭了firefox,ps看不到一个firefox进程,第二次再启动的时候就比第一次明显快很多,这是由于这次系统是直接从cache中读取的firefox来运行,并不是从磁盘上读取的。

    再有一个例子:我们频繁使用的ls命令等基本命令,你运行的时候根本看不到硬盘灯闪,因为这些常用的命令都是再第一次运行后就保存在cache中的,以后就一直从内存中读出来运行。

    如果cache占用的内存过多了,影响正常运行程序需要的内存,那么会释放掉一部分cache内存,但是总量会保持一个很高的值,所以,linux总是能最大限度的使用内存,就算加到16G,32G内存,也会随着不断的IO操作,内存的free值会慢慢减少到只有几M,想要内存不发生这种情况,只有一个办法:把内存加到比硬盘大。

    2 手动释放方法

    2.1 使用free查看一下当前内存使用情况(可略过):

    [root@*** ~]# free -m
    total       used       free     shared    buffers     cached
    Mem:           512        488         23          0         57        157
    -/+ buffers/cache:        273        238 Swap:         1055          0       1055

    2.2 执行sync同步数据

    [root@*** ~]# sync

    2.3 清理cache

    [root@*** ~]#echo 3 > /proc/sys/vm/drop_caches

    2.4 drop_cache的详细文档如下,以便查阅

    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.

    网上查了查资料,整理一下:

    关于drop_caches文件:系统默认为0

    在Documentation/sysctl/vm.txt中有如下描述:

    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:仅清除页面缓存(PageCache)
     echo 1 > /proc/sys/vm/drop_caches

    To free dentries and inodes:清除目录项和inode
     echo 2 > /proc/sys/vm/drop_caches

    To free pagecache, dentries and inodes:清除页面缓存,目录项和inode
     echo 3 > /proc/sys/vm/drop_caches

    As this is a non-destructive operation and dirty objects are not freeable, the user should run `sync' first.

  • 相关阅读:
    Sun:收购MySQL是现代软件史上最重要收购[ZT]
    SCI2012年收录的中文期刊
    Elsevier期刊投稿状态
    医学图像SCI
    贝叶斯法则,先验概率,后验概率,最大后验概率
    医学图像处理与分析方面的大牛
    2013 EI检索的国内期刊
    ICIP EMBC IUS 2013
    香港中文大学第六十九届颁授学位典礼 校长赠言 我默祷你们都能不负此生
    自动生成参考文献编号
  • 原文地址:https://www.cnblogs.com/wuzm/p/11812328.html
Copyright © 2020-2023  润新知