zoukankan      html  css  js  c++  java
  • linux内存管理及手动释放机制

    inux系统中查看内存状态一般都会用到free

    linux的free命令中,cached和buffers的区别
    Free
    Mem:表示物理内存统计
      -/+ buffers/cached:表示物理内存的缓存统计
      Swap:表示硬盘上交换分区的使用情况
      系统的总物理内存:8098060 8Gb,但系统当前真正可用的内存并不是第一行free 标记的6054972Kb,它仅代表未被分配的内存。
      我们使用total1、used1、free1、used2、free2 等名称来代表上面统计数据的各值,1、2 分别代表第一行和第二行的数据。
      total:表示物理内存总量。
      used:表示总计分配给缓存(包含buffers 与cache )使用的数量,但其中可能部分缓存并未实际使用。
      free:未被分配的内存。
      shared:共享内存,一般系统不会用到,这里也不讨论。
      buffers:系统分配但未被使用的buffers 数量。
      cached:系统分配但未被使用的cache 数量。buffer 与cache 的区别见后面。
           note:
           total=used+free
           used=buffers+cached (maybe add shared also)
    buffer 与cache 的区别
    A buffer is something that has yet to be "written" to disk. A cache is something that has been "read" from the disk and stored for later use.
      对于共享内存(Shared memory),主要用于在UNIX 环境下不同进程之间共享数据,是进程间通信的一种方法,一般的应用程序不会申请使用共享内存,笔者也没有去验证共享内存对上面等式的影响。
    cache 和 buffer的区别:
     Cache:高速缓存,是位于CPU与主内存间的一种容量较小但速度很高的存储器。由于CPU的速度远高于主内存,CPU直接从内存中存取数据要等待一定时间周期,Cache中保存着CPU刚用过或循环使用的一部分数据,当CPU再次使用该部分数据时可从Cache中直接调用,这样就减少了CPU的等待时间,提高了系统的效率。Cache又分为一级Cache(L1 Cache)和二级Cache(L2 Cache),L1 Cache集成在CPU内部,L2 Cache早期一般是焊在主板上,现在也都集成在CPU内部,常见的容量有256KB或512KB L2 Cache。
      Buffer:缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据的区域。通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据时,速度快的设备的操作进程不发生间断。
      Free中的buffer和cache:(它们都是占用内存):
      buffer : 作为buffer cache的内存,是块设备的读写缓冲区
      cache: 作为page cache的内存, 文件系统的cache
      如果 cache 的值很大,说明cache住的文件数很多。如果频繁访问到的文件都能被cache住,那么磁盘的读IO bi会非常小
     
    内存释放机制
    关于drop_caches的官方说明如下:

    Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to becomefree.

    To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;

    to free 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.


    /proc是一个虚拟文件系统,我们可以通过对它的读写操作做为与kernel实体间进行通信的一种手段.也就是说可以通过修改/proc中的文件,来对当前kernel的行为做出调整.那么我们可以通过调整/proc/sys/vm/drop_caches来释放内存.操作如下:
    #cat /proc/sys/vm/drop_caches
    0
    首先,/proc/sys/vm/drop_caches的值,默认为0
    手动执行sync命令
    描述:sync 命令运行 sync 子例程。如果必须停止系统,则运行 sync 命令以确保文件系统的完整性。sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件
    #sync
    #echo 3 > /proc/sys/vm/drop_caches
    #cat /proc/sys/vm/drop_caches
    3
    将/proc/sys/vm/drop_caches值设为3
    再次执行free查看内存已释放完毕。

  • 相关阅读:
    LyX – The Document Processor
    An HTML5 presentation builder — Read more
    R语言定义
    A Hybrid User and ItemBased Collaborative Filtering with Smoothing on Sparse Data
    RapidMiner
    http://www.rseek.org一个查找R资料的站点
    An HTML5 presentation builder — Read more
    R代码脚本的运行
    data ming with R a book
    A Hybrid User and ItemBased Collaborative Filtering with Smoothing on Sparse Data
  • 原文地址:https://www.cnblogs.com/suway/p/7236416.html
Copyright © 2011-2022 走看看