Linux memory check

Reprinted from: http://www.cnblogs.com/chenshoubiao/p/4796664.html

 

 free query available memory

The free tool is used to view the available memory of the system:

/opt/app/tdev1$free
             total       used       free     shared    buffers     cached
Mem:       8175320    6159248    2016072          0     310208    5243680
-/+ buffers/cache:     605360    7569960
Swap:      6881272      16196    6865076

Explain the output of free command on Linux.

The following is the running result of free, a total of 4 lines. I have added column numbers for convenience. In this way, the output of free can be regarded as a two-dimensional array FO (Free Output). E.g:

FO[2][1] = 24677460
FO[3][2] = 10321516

                   1          2          3          4          5          6
1              total       used       free     shared    buffers     cached
2 Mem:      24677460   23276064    1401396          0     870540   12084008
3 -/+ buffers/cache:   10321516   14355944
4 Swap:     25151484     224188   24927296

The output of free has a total of four lines. The fourth line is the information of the swap area, which are the total amount of swap (total), the amount of usage (used), and the amount of free swap area (free). .

The second and third lines of the free output are confusing. Both lines describe memory usage. The first column is total, the second is used, and the third is free.

  The output of the first line is viewed from the operating system (OS). That is, from an OS point of view, there are a total of:

24677460KB (the default unit of free is KB) physical memory, namely FO[2][1]; 23276064KB (ie FO[2][2]) are used in these physical memories; 1401396KB (ie FO [2][3]) is available;

Here we get the first equation:

FO[2][1] = FO[2][2] + FO[2][3]

FO[2][4] represents the memory shared by several processes, which has now been deprecated, and its value is always 0 (of course, it may not be 0 on some systems, depending on how the free command is implemented).

FO[2][5] represents the memory buffered by the OS. FO[2][6] represents the memory cached by the OS. In some cases the terms buffer and cache are often used interchangeably. However, in some relatively low-level software, it is necessary to distinguish these two words, see the foreign language of foreigners:

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.

That is to say, the buffer is used to store the data to be output to the disk (block device), and the cache is used to store the data read from the disk. These two are for improving IO performance and are managed by the OS.

Linux和其他成熟的操作系统(例如windows),为了提高IO read的性能,总是要多cache一些数据,这也就是为什么FO[2][6](cached memory)比较大,而FO[2][3]比较小的原因。我们可以做一个简单的测试:

释放掉被系统cache占用的数据:

echo 3>/proc/sys/vm/drop_caches
  1. 读一个大文件,并记录时间;
  2. 关闭该文件;
  3. 重读这个大文件,并记录时间;

第二次读应该比第一次快很多。原来我做过一个BerkeleyDB的读操作,大概要读5G的文件,几千万条记录。在我的环境上,第二次读比第一次大概可以快9倍左右。

free输出的第二行是从一个应用程序的角度看系统内存的使用情况。

  • 对于FO[3][2],即-buffers/cache,表示一个应用程序认为系统被用掉多少内存;
  • 对于FO[3][3],即+buffers/cache,表示一个应用程序认为系统还有多少内存;

因为被系统cache和buffer占用的内存可以被快速回收,所以通常FO[3][3]比FO[2][3]会大很多。

这里还用两个等式:

FO[3][2] = FO[2][2] - FO[2][5] - FO[2][6] FO[3][3] = FO[2][3] + FO[2][5] + FO[2][6] 

这二者都不难理解。

free命令由procps.*.rpm提供(在Redhat系列的OS上)。free命令的所有输出值都是从/proc/meminfo中读出的。

在系统上可能有meminfo(2)这个函数,它就是为了解析/proc/meminfo的。procps这个包自己实现了meminfo()这个函数。可以下载一个procps的tar包看看具体实现,现在最新版式3.2.8。





10. vmstat 监视内存使用情况

vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可实时动态监视操作系统的虚拟内存、进程、CPU活动。

10.1. vmstat的语法

  vmstat [-V] [-n] [delay [count]]

  • -V表示打印出版本信息;
  • -n表示在周期性循环输出时,输出的头部信息仅显示一次;
  • delay是两次输出之间的延迟时间;
  • count是指按照这个时间间隔统计的次数。
/root$vmstat 5 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
6  0      0 27900472 204216 28188356    0    0     0     9    1    2 11 14 75  0  0
9  0      0 27900380 204228 28188360    0    0     0    13 33312 126221 22 20 58  0  0
2  0      0 27900340 204240 28188364    0    0     0    10 32755 125566 22 20 58  0  0

10.2. 字段说明

Procs(进程):
  • r: 运行队列中进程数量
  • b: 等待IO的进程数量
Memory(内存):
  • swpd: 使用虚拟内存大小
  • free: 可用内存大小
  • buff: 用作缓冲的内存大小
  • cache: 用作缓存的内存大小
Swap:
  • si: 每秒从交换区写到内存的大小
  • so: 每秒写入交换区的内存大小
IO:(现在的Linux版本块的大小为1024bytes)
  • bi: 每秒读取的块数
  • bo: 每秒写入的块数
system:
  • in: 每秒中断数,包括时钟中断
  • cs: 每秒上下文切换数
CPU(以百分比表示)
  • us: 用户进程执行时间(user time)
  • sy: 系统进程执行时间(system time)
  • id: 空闲时间(包括IO等待时间)
  • wa: 等待IO时间

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326359109&siteId=291194637