This article transferred from: http://www.cnblogs.com/coldplayerest/archive/2010/02/20/1669949.html
Explain the output of the free command on Linux.
Here is the result of the free run, with a total of 4 rows. For convenience, I added a column number. This can be seen as a two-dimensional array fo (free output). For example:
- 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 behavior Swap area information, which is the total amount of the exchange, the amount of usage (used), and the number of idle swap areas, which are relatively clear, not too much.
The second and third rows in the free output are confusing. Both of these lines describe memory usage. The first column is total, the second column is the usage (used), and the third column is the available amount (free).
The first line of output is viewed from the operating system (OS). That is, from the OS point of view, the computer has a total of:
- 24677460KB (the default is KB for free) physical memory, or fo[2][1];
- 23276064KB (i.e. fo[2][2]) is used in these physical memory;
- Also used 1401396KB (i.e. fo[2][3]) is available;
Here we get the first equation:
- FO[2][1] = fo[2][2] + fo[2][3]
FO[2][4] indicates that the memory that is shared by several processes is now deprecated, and its value is always 0 (it may not be 0 on some systems, depending on how the free command is implemented).
FO[2][5] represents the memory that is trapped by the OS buffer. FO[2][6] Indicates the memory of the OS cache. In some cases, the two words buffer and cache are often mixed. But in some low-level software is to distinguish between the two words, look at the foreigner's Foreign Language:
- A buffer is something that have yet to being "written" to disk.
- A cache is something that have been "read" from the disk and stored for later use.
That is, buffer is used to store the data to be output to disk (block device), and the cache is to store the data read from 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 some data, which is why Fo[2][6] (cached memory) is relatively large, and fo[2][3] relatively small reason. We can do a simple test:
- Release the data that was consumed by the system cache; Echo 3>/proc/sys/vm/drop_caches
- Read a large file and record the time;
- Close the file;
- Reread the large file and record the time;
The second reading should be much faster than the first time. Originally I did a berkeleydb read operation, about to read 5G files, tens of millions of records. In my environment, the second reading is about 9 times times faster than the first time.
The second line of the free output is the use of system memory from an application perspective.
- For fo[3][2], or-buffers/cache, indicates how much memory an application thinks the system is using;
- For fo[3][3], or +buffers/cache, indicates how much memory an application thinks the system has;
Because the memory consumed by the system cache and buffer can be quickly recycled, fo[3][3] is usually much larger than fo[2][3].
It also uses two equations:
- FO[3][2] = fo[2][2]-fo[2][5]-fo[2][6]
- FO[3][3] = fo[2][3] + fo[2][5] + fo[2][6]
Both are not difficult to understand.
The free command is provided by PROCPS.*.RPM (on the OS of the Redhat series). All output values of the free command are read from the/proc/meminfo.
There may be meminfo (2) This function on the system, it is to parse/proc/meminfo. Procps This package itself implements the Meminfo () function. You can download a Procps tar package to see the specific implementation, now the latest layout 3.2.8.
Free to view available caches