Page cache and buffer cache in Linux systems

Source: Internet
Author: User
Tags map data structure

Linux page cache in the system and Buffer Cache

Free Command Display Memory

First, let's look at the usage of the memory:

650) this.width=650; "title=" clip_image001 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px, "alt=" clip_image001 "src=" Http:// "border=" 0 "height=" 425 "/>

Mem: Indicates physical memory statistics

Total: Indicates the amount of physical memory (totals = used + Free)

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.

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.

-/+ Buffers/cache: Cache statistics representing physical memory

Used2: The used–buffers-cached in the first row is also the amount of memory actually used. Used2 as the second row

Free2= buffers1 + cached1 + free1//free2 for the second row, buffers1, etc. as the first row

Free2: The sum of unused buffers with the cache and unallocated memory, which is the current actual memory available to the system.

Swap: Indicates the usage of the swap partition on the hard disk.

The buffer and cache are displayed in the free command, and they are all memory-intensive:

Buffer: The memory of buffer cache is the read-write buffer of the block device, closer to the storage device, or directly to disk's buffer.

Cache: The memory of the page cache, the cache of the file system, is the buffer of memories.

If the cache has a large value, it indicates that the cache has a high number of files. If frequently accessed files can be cache, then the disk read IO must be very small.

Page Cache (page cache)

Page cache is also called paged or file buffering, is composed of several disk blocks, the size is usually 4k, on a 64-bit system is 8k, the composition of several disk blocks on the physical disk is not necessarily contiguous, the file's organizational unit is a page, that is, a pages cache size, A file read is a few contiguous disk blocks on the external memory, to buffer cache, then the page cache, which is then supplied to the application.

When the Page cache reads and writes files to Linux, it is used to cache the logical contents of the file, thus speeding up access to the disk's image and data. Specifically, to speed up access to the contents of the file, buffer caches the contents of the file-the disk block on the physical disk, which accelerates access to the disk.

Buffer Cache (block Cache)

Buffer cache is also called a block buffer, which is the buffer of a disk block on a physical disk, which is typically 1k in size and the disk block is also the organizational unit of the disk. The purpose of the buffer cache is to reduce access time when the program accesses the same disk block multiple times. The system reads the disk block first into the buffer cache if the cache space is insufficient, some outdated or multiple unused buffer caches are emptied by a certain policy. The next time the program accesses the disk, it first checks to see if the desired block is found in buffer cache, and the hit reduces the time to access the disk. The buffer cache needs to be read again when it is not hit. The buffer cache is written into two, one is to write directly, this is the program in the buffer cache after writing disk, to read from the buffer cache, the second is the background write, the program after the buffer cache does not immediately write disk, Because it is possible for the program to write files in a short time, if you write directly, you need to write the disk more than once. This is inefficient, but after a period of time written by the background, reducing the number of times to visit the disk.

The buffer cache is allocated by physical memory, and the Linux system will allocate free memory to the buffer cache for increased memory usage, and the system will reduce the CAHCE size when other programs require more memory.

Buffer Page (Buffer page)

If the kernel needs to access a block individually, the buffer page is involved and the corresponding buffer head is checked.

Swap Space (swap space)

Swap space is the representation of virtual memory. In order to cope with some applications that require large amounts of memory, the system uses the space on the disk as memory, and when physical memory is not enough, exchange some of the temporarily unwanted data to swap space, also known as swap files or paging files. The benefit of doing virtual memory is to make the process feel as if it can access the entire system's physical memory. Because when a process accesses data, the data of other processes is swapped into the swap space.

Swap Cache (swap cache)

A swapcached that represents the size of the swap cache. The Page cache is a cache of disk data in memory, while the swap cache is a temporary cache of swap partitions in memory.

Memory Mapping (Memory-mapped)

The kernel has two types of memory mappings: Shared and private. Private mapping is more efficient when the process is used for read-only files without writing files. However, any write to a private mapped page causes the kernel to stop mapping pages in that file. Therefore, the write operation does not alter the files on the disk and is not visible to other processes that access the file.

Pages in shared memory are typically located in page cache, which is also located in page cache as long as the private memory map is not modified. When the process attempts to modify a private mapped memory page, the kernel copies the page and replaces the original page with the copied page in the page table. Because the page table was modified, although the original page is still in page cache, it is no longer part of the memory map. The newly copied pages are not inserted into the page cache, but instead are added to the anonymous pages of the inverse map data structure.

Page Cache and Buffer Cache the Difference

The operations of a disk are logical (file system) and physical (disk blocks), which are caches of logical and physical-level data, respectively.

Assuming that we manipulate the file through the file system, the file will be cached to page cache, and if the file needs to be refreshed, the page cache will be given to buffer cache to complete because the buffer cache is the cached disk block.

That is, directly to the operation of the file, that is, the page cache buffer, with the command of DD and other direct operation of the disk block, is buffer cache cached things.

The page cache is actually for the file system, the file cache, and the data at the file level is cached to page cache. The logical layer of the file needs to be mapped to the actual physical disk, and this mapping is done by the file system. When the page cache's data needs to be refreshed, the data in the page cache is given to buffer cache, but this processing is simple after the 2.6 kernel, with no real cache operation.

Buffer cache is a caching of disk blocks that, in the absence of a filesystem, caches data directly into the buffer cache, for example, the file system's metadata is cached in 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, the data is cached to the page cache, and if the disk is read and written directly using tools such as DD, the data is cached to buffer cache.

Buffer cache Buffers The operation of block devices in blocks, timing or manual synchronization to the hard disk, it is to buffer the write operation and then one time to write a lot of changes to the hard disk, avoid frequent write hard disk, improve write efficiency.

The cache page caches the file system files and reads them to the program that needs to be used, which provides buffering for read operations, avoids frequent hard disk reads, and improves read efficiency.

This article is from the SQL Server deep dives blog, so be sure to keep this source

Page cache and buffer cache in Linux systems

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.