Detailed explanation of free command on Linux

Reprinted from: http://www.cnblogs.com/coldplayerest/archive/2010/02/20/1669949.html

The free command checks the memory usage status. The default output unit is KB.
free -b bytes output, -k KB output, -m MB output, -h human-readable output, automatically display GM, etc.


Get skills:
1. Commonly used: free -m
2. When viewing memory from an application perspective, check the -/+ buffers/cache line.
3. Both IO read and write have cache. When the test file is read and written, the subsequent read and write will be much faster than the first time. You can use

echo  3 >/ proc / sys / vm / drop_caches   Clear the cache and eliminate the impact.


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 unit of free is KB by default) physical memory, namely FO[2][1];
  • In these physical memory, 23276064KB (ie FO[2][2]) is used;
  • Also 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 and other mature operating systems (such as windows), in order to improve the performance of IO read, always cache some more data, which is why FO[2][6] (cached memory) is relatively large, and FO[2] [3] for relatively small reasons. We can do a simple test:

  1. Release the data occupied by the system cache;
    echo  3 >/ proc / sys / vm / drop_caches

     

  2. Read a large file and record the time;
  3. close the file;
  4. Reread this large file and record the time;

The second read should be much faster than the first. It turns out that I have done a BerkeleyDB read operation, which probably needs to read 5G files and tens of millions of records. In my environment, the second read was about 9 times faster than the first.

  The second line of free output is the system memory usage from an application's point of view.

  • 对于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。


Guess you like

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