The free command

  
in Linux explains the output of the free command on Linux. The following is the result of the free run, a total of 4 lines. For the convenience of explanation, I added the column number. This can treat the free output as a two-dimensional array FO (Free Output). For example: FO[2][1] = 15402628 FO[3][2] = 12033012 The output of free has a total of four rows, and the fourth behavior is the information of the exchange area, which is the total amount of exchange (total), usage (used ) And how many free exchange areas (free), this is clear, not too much. The second and third lines of the free output are confusing. Both lines describe the memory usage. The first column is the total, the second column is used, and the third column is free. The output of the first line is viewed from the operating system (OS). That is to say, from the perspective of the OS, there are a total of 15402628KB (the default free unit is KB) physical memory, that is, FO[2][1]; there are 15318812KB in these physical memories (ie FO[ ,null,null,3],2][2]) is used; also uses 83816KB (ie FO[2][3]) is available;

here gets the first equation:
FO[2][1] = FO [2][2] + FO[2][3]

FO[2][4] indicates that the memory shared by several processes is now deprecated, and its value is always 0 (of course on some systems too) May not be 0, mainly depends on how the free command is implemented).

FO[2][5] represents the memory that is occupied by the OS buffer. FO[2][6] indicates the memory of the OS cache. In some cases, the words buffer and cache are often mixed. However, in some lower-level software, it is necessary to distinguish these two words, look at the foreigner's foreign language:
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, the buffer is used to store the data to be output to the disk (block device), and the cache is to store the data read from the disk. Both are designed to improve 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 have to cache more data, which is why FO[2][6](cached memory) is relatively large And FO[2][3] is a small reason. We can do a simple test:

1. Free up 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 the large file and record the time;

The second read should be better than The first time is a lot faster. I used to do a BerkeleyDB read operation, read 5G files, tens of millions of records. In my environment, the second reading is about 9 times faster than the first.

The second line of free output is the use of system memory from the perspective of an application.
For FO[3][2], ie -buffers/cache, indicates how much memory an application thinks the system is using; for FO[3][3], ie +buffers/cache, an application thinks How much memory is in the system;

Because the memory occupied by the system cache and buffer can be quickly reclaimed, usually FO[3][3] is much larger than FO[2][3].

There are two equations here:
FO[3][2] = FO[2][2] - FO[2][5] - FO[2][6]FO[ ,null,null,3],3][3] = FO[2][3] + FO[2][5] + FO[2][6]

These two are not difficult to understand.

The free command is provided by procps.*.rpm (on the Redhat series OS). All output values ​​of the free command are read from /proc/meminfo.

There may be a meminfo(2) function on the system, which is to parse /proc/meminfo. The procps package implements the meminfo() function itself. You can download a procps tar package to see the specific implementation, now the latest version 3.2.8.

Copyright © Windows knowledge All Rights Reserved