The free command in Linux

Source: Internet
Author: User

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] = 15402628 fo[3][2] = 12033012

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:

    • 15402628KB (the default is KB for free) physical memory, or fo[2][1];
    • 15318812KB (i.e. fo[2][2]) is used in these physical memory;
    • Also used 83816KB (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:

1. Release the data that was consumed by the system cache;

Echo 3>/proc/sys/vm/drop_caches

2. Read a large file and record the time;

3. Close the document;

4. Reread the large document 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.

Some time ago a project was written in C, and the performance test found a memory leak. Free is a good command for how to observe memory usage issues.

bash-3.00$ Free
Total used free shared buffers Cached
mem:1572988 1509260 63728 0 62800 277888
-/+ buffers/cache:1168572 404416
swap:2096472 16628 2079844

Mem: Indicates physical memory statistics
-/+ buffers/cached: Cache statistics representing physical memory
Swap: Indicates the usage of the swap partition on the hard disk, we don't care about it here.
Total physical memory of the system: 255268Kb (256M), but the system is currently really available memory B is not the first line of the free Mark 16936Kb, it represents only unallocated memory.

Line 1th Mem:total: Represents the total amount of physical memory.
Used: Represents the amount that the total is allocated to the cache (including buffers and cache), but may not be actually used in some of the caches.
Free: Memory that has not been allocated.
GKFX: Shared memory, general system will not be used, nor discussed here.
Buffers: The number of buffers that are allocated but not used by the system.
Cached: The number of caches that the system allocates but has not been used. The difference between buffer and cache is shown later. Total = used + Free line 2nd-/+ buffers/cached:used: The used-buffers-cached in the first row is also the amount of memory actually used.
Free: The sum of unused buffers with the cache and unallocated memory, which is the actual memory available to the system at the moment. Free 2= buffers1 + cached1 + free1//free2 for the second row, buffers1, etc. as the first line

The difference between buffer and cache
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 line 3rd: The third line refers to the application from the point of view, BUFFERS/CA Ched is equal to available, because buffer/cached is designed to improve the performance of file reads, and buffer/cached is quickly recycled when applications need to use memory.
So from the application's point of view, available memory = System Free memory+buffers+cached.

Next, explain when the memory will be exchanged, and by what side.

When the available memory is less than the rated value, a meeting is exchanged.
How to look at the rated value (RHEL4.0):
#cat/proc/meminfo
The exchange will reduce the number of physical pages used in the system in three ways:
1. Reduce the buffer and the size of the page cache,
2. Swap out the memory page of the System V type
3. Swap out or discard the page. (Application occupies a memory page, that is, insufficient physical memory).
In fact, a small amount of swap use does not affect the performance of the system.

Here is the difference between buffers and cached.

Buffers is the buffer size used to make a block device, and he only records the metadata of the filesystem and tracking in-flight pages.
Cached is used to buffer files.
That is to say: buffers is used to store, what content inside the directory, permissions and so on.
and cached directly to the memory of our open files, if you want to know if he is really effective, you can try, successively executed two times the command #man X, you can obviously feel that the second opening is much faster.
Experiment: Doing something on a machine with no application can be seen more clearly. Remember that the experiment can only be done once, if you want to do more please change a file name.
#free
#man X
#free
#man X
#free
You can compare the size of the buffers after the free.
Another experiment:
#free
#ls/dev
#free
You compare the size of two, of course, this buffers is increasing at any time, but you have the LS, the increase in speed will become faster, this is the difference between buffers/chached.

Because Linux uses your temporary unused memory as a file and data cache to improve system performance, when you need these memory, the system will automatically release (unlike Windows, even if you have a lot of free memory, he will have access to the disk pagefiles)
Use the free command
Subtracting the value of buffer and cache from the value of used is your current real memory usage ———— – A parameter that is mem for the operating system. Buffers/cached are all part of being used, so it thinks free is only 16936.

For the application It is (-/+ Buffers/cach). buffers/cached is equivalent, because buffer/cached is designed to improve the performance of the program execution, and when the program uses memory, buffer/cached is quickly used. So, take a look at the application, to (-/+ Buffers/cache) of the free and used-based. That's why we look at this. In addition to tell you some common sense. In order to improve disk and memory access efficiency, Linux has done a lot of careful design, in addition to the Dentry cache (for VFS, speed up the file path name to Inode conversion), but also adopted two main cache mode: Buffer cache and Page cache. The former is for the disk block read and write, the latter for the file inode read and write. These caches can effectively shorten the time for I/O system calls (such as read,write,getdents). Remember that memory is for use, not to take a look. Unlike Windows, no matter how much real physical memory you have, he will have to take the hard disk swap files to read. This is why Windows often prompts for a lack of virtual space. You think, how boring, in memory and most of the time, Take out some of the hard disk space to act as memory. So we look at Linux, as long as the swap space without swap, there is no need to worry about their memory too little. If you use a lot of swap, you might want to consider adding physical memory. This is the standard for Linux to see if memory is adequate.

———————————————
Syntax: free [-bkmotv][-s < interval of seconds;]

Additional Note: The free instruction shows memory usage, including physical memory, virtual swap file memory, shared memory segments, and buffers used by the system core.

Parameters
-B Displays memory usage in bytes.
-K displays memory usage in kilobytes.
-m displays memory usage in megabytes.
-O does not display buffer throttling columns.
-s< interval seconds > Continuous observation of memory usage.
-T displays the memory sum column.
-V Displays version information.

The free command in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.