Real memory used by Linux

Source: Internet
Author: User

The Linux and Windows we use are not the same. The top command may not actually use the memory. The second line of the free command is the actual memory used by the system. Don't panic if you find that your memory is full of PHP-CGI.

Page cache and buffer cache have always been two confusing concepts. On the Internet, many people are arguing about the differences between the two caches, at the end of the discussion, there has never been a unified and correct conclusion. During my work, the concepts of page cache and buffer cache have plagued me, but after careful analysis, these two concepts are actually very clear. If we can understand the nature of these two caches, we may be more comfortable in analyzing Io problems.

The page cache is actually for the file system, and is the File Cache. Data at the file level is cached in the page cache. The logic layer of the file needs to be mapped to the actual physical disk. This ing relationship is completed by the file system. When the data in the page cache needs to be refreshed, the data in the page cache is handed over to the buffer cache. However, this kind of processing becomes very simple after the kernel of version 2.6, and there is no real cache operation.
Buffer cache is the cache for disk blocks, that is, if no file system is available, data directly operated on the disk will be cached in the buffer cache. For example, the metadata of the file system is cached in the buffer cache.
In short, page cache is used to cache file data, and buffer cache is used to cache disk data. In the case of a file system, operations on the file will cache the data to the page cache. If you directly use dd or other tools to read and write the disk, the data will be cached to the buffer cache.
In addition, at the file system layer, each device is assigned a file operation method of def_blk_ops. This is the operation method of the device. A radix tree exists under the inode of each device, the page of the cached data will be placed under this Radix tree. The number of pages is displayed in the buffer column of the top program. If the device has a file system, an inode will be generated, and the inode will allocate operation methods such as ext3_ops. These methods are the file system methods, there is also a radix tree under this inode, where the page of the file is cached, and the number of cached pages is calculated in the cache column of the top program. From the above analysis, we can see that the buffer cache and page cache in the 2.6 kernel are consistent in terms of processing, but there are conceptual differences. Page cache is for File Cache, buffer is the cache for disk block data.
Isn't it all about page cache? Buffer pages are actually pages in the page cache. It only adds an abstraction layer and uses buffer_head to perform some access management.
Right, from the perspective of Linux Algorithm Implementation, the page cache and buffer cache are the same, but there are still differences between the two in terms of functional abstraction and specific applications, this can be seen from the statistics of the top tool. Pay attention to the two statistics of buffer and cache.
Add some materials:
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.
Enter: free in the terminal
Display: Total used free shared buffers cached
Mem: 255268 238332 16936 0 85540 126384
-/+ Buffers/cache: 26408 228860
The total physical memory of the system is 255268kb (256 MB). However, the actual available memory of the system is not 16936kb in the first line. It only indicates the unallocated memory.
We use names such as total1, used1, free1, used2, and free2 to represent the values of the preceding statistics. 1 and 2 represent the data of the first and second rows respectively.
Total1: total physical memory.
Used1: indicates the total quantity allocated to the cache (including buffers and cache), but some of the caches are not actually used.
Free1: unallocated memory.
Shared1: shared memory, which is not used by the general system and is not discussed here.
Buffers1: Number of buffers allocated by the system but not used.
Cached1: Number of cache allocated by the system but not used. The difference between buffer and cache is described later.
Used2: the total amount of actually used buffers and cache, which is also the total amount of actually used memory.
Free2: The sum of unused buffers, cache, and unallocated memory, which is the actual available memory of the system.
The following equations can be sorted out:
Total1 = used1 + free1
Total1 = used2 + free2
Used1 = buffers1 + cached1 + used2
Free2 = buffers1 + cached1 + free1

Post from: Linux community

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.