Linux内存监控工具

时间:2022-03-13 14:36:35

本文为转载 http://www.opensolution.org.cn/archives/502.html

一、free

  该工具主要是显示系统里可用和已用的内存

Linux内存监控工具

Linux通常按一定的算法把常用的数据加载到系统的虚拟内存bufferscached中,以便于用户程序在访问系统资源更快。而由free 查看到的buffers是用于存放元数据,而cached是用于存放真实的文件内容。

 

由上图free -k的输出结果中可知:

系统总物理内存(total)4144656K(4G);

已用(Mem行对应的used)的物理内存3871932K(3.8G,:这里包含了buffers152460K(152M)cached2253060K(2.2G).),他包含系统的bufferscached的。

-/+ buffers/cache对应的used1466412K(1.4G),也就是Memused(3871932K)-Membuffers(152460K)-

Memcached(2253060K)=1466412K(1.4G).所以实际上可用于分配的物理内存(-/+ buffers/cache行对应的free)2678244K(2.6G).

Sharedman手册里提示应该忽略(man free:The shared memory column should be ignored; it is obsolete.)

Mem行对应的free对应的274220K(274M).其实这个free是有一定限制的:不能低于min_free_kbytes

min_free_kbytes用于计算系统里lowmem zone(物理内存0-896MB之间的zone)的值(This is used to force the Linux VM to keep a minimum number of kilobytes free.  The VM uses this number to compute a pages_min value for each lowmem zone in the system.  Each lowmem zone gets a number of reserved free pages based proportionally on its size.).

计算方式参见mm/page_alloc.cmin_free_kbytes = sqrt(lowmem_kbytes * 16)

 

上述值是一定的公式计算

系统的lowmem872656KB

[root@crm_10 /root]grep LowTotal /proc/meminfo

LowTotal:  872656

min_free_kbytes=sqrt(872656*16) 约等于 3797

二、ps,top

  这两个工具在内存监视方面有很大的相似性,所以一并说一下:

下面top里的VIRT相当于ps 里的VSZ:指用于这个任务的总虚拟内存(虚拟内存包括物理内存和swap交换分区),包括所有的代码、数据、共享库以及已经被outswap分区的数据。/* The total amount of virtual memory used by the task.  It includes all code, data and shared libraries plus pages that have been swapped out.*/

 

top里的RES 相当于ps 里的RSS: 指用于这个任务的没被outswap分区的总物理内存/* resident set size, the non-swapped physical memory that a task has used */

top里的%MEM: 指这个任务的RES占总物理内存的比率/* Memory usage (RES) A task's currently used share of available physical memory.*/

三、vmstat

 

显示的值跟用free工具查看到的值相似。一般情况下:只要swap一列的si/so数值不超过1024即可。

Swap

       si: Amount of memory swapped in from disk (/s).

       so: Amount of memory swapped to disk (/s).

:VFS里的meminfo信息:

Linux内存监控工具

Dirty是指数据已写入内存,但还没同步到外存(磁盘)的数据量.

Slab:为了提供内核空间以页分配对有些调用(只需小内存)不合适的一种内存分配方式,提出Pool的概念。

Vmalloc:为了解决非连续性内存的使用,提供的一种内存分配方式(采用链表)

CommitLimit:指当前可以分配给程序使用的虚拟内存(只有当vm.overcommit_memory的值设置为2时,CommitLimit才有意义)

 CommitLimit: Based on the overcommit ratio ('vm.overcommit_ratio'),
              this is the total amount of  memory currently available to
              be allocated on the system. This limit is only adhered to
              if strict overcommit accounting is enabled (mode 2 in
              'vm.overcommit_memory').
              The CommitLimit is calculated with the following formula:
              CommitLimit = ('vm.overcommit_ratio' * Physical RAM) + Swap
              For example, on a system with 1G of physical RAM and 7G
              of swap with a `vm.overcommit_ratio` of 30 it would
              yield a CommitLimit of 7.3G.
              For more details, see the memory overcommit documentation
              in vm/overcommit-accounting.

 

Committed_AS:指当前已分配给程序使用的总虚拟内存(包含已分配给进程但还没使用的内存)

Committed_AS: The amount of memory presently allocated on the system.
              The committed memory is a sum of all of the memory which
              has been allocated by processes, even if it has not been
              "used" by them as of yet. A process which malloc()'s 1G
              of memory, but only touches 300M of it will only show up
              as using 300M of memory even if it has the address space
              allocated for the entire 1G. This 1G is memory which has
              been "committed" to by the VM and can be used at any time
              by the allocating application. With strict overcommit
              enabled on the system (mode 2 in 'vm.overcommit_memory'),
              allocations which would exceed the CommitLimit (detailed
              above) will not be permitted.
This is useful if one needs
              to guarantee that processes will not fail due to lack of
              memory once that memory has been successfully allocated.

 

 

HugePagesize:X86架构下,通常linux给内存分页时,默认是每页是4KB,而有些应用自己可以管理内存(例如db,java……),所以可以把页的大小分大些(32位下:4K4M,PAE模式下可以把每页设置为2M.64位下: 4K, 8K, 64K, 256K, 1M, 4M, 16M,256M),这样可以增加TLB(一个存储线性地址和物理地址对应表的高速缓冲器)存储的条目,这样就可减少线性地址到物理地址转换的过程.可通过vm.hugetlb_shm_group vm.nr_hugepages参数进行调整。具体可查看/usr/share/doc/kernel-doc-`uname –r|cut -d- -f1`/Documentation/vm/hugetlbpage.txt(如果你的机器上已经安装了kernel-doc)

 

The intent of this file is to give a brief summary of hugetlbpage support in

the Linux kernel.  This support is built on top of multiple page size support

that is provided by most of modern architectures.  For example, IA-32

architecture supports 4K and 4M (2M in PAE mode) page sizes, IA-64

architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M,

256M.  A TLB is a cache of virtual-to-physical translations.  Typically this

is a very scarce resource on processor.  Operating systems try to make best

use of limited number of TLB resources.  This optimization is more critical

now as bigger and bigger physical memories (several GBs) are more readily

available.