What is the difference between buffer and cache? Let's start with the free command.
Free
Compared with top, the free command provides a more concise view of the system memory usage:
$ Free
total used free shared buffers cached
Mem: 255268 238332 16936 0 85540 126384
-/+ Buffers/cache: 26408 228860 swap: 265000 0 265000
Mem: physical memory statistics
-/+ Buffers/cached: indicates the cache statistics of physical memory.
Swap: indicates the usage of swap partitions on the hard disk.
The total physical memory of the system is 255268kb (256 MB), but the actually available memory B of the system is not 16936kb In the first line, which 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 + free1total1 = used2 + free2used1 = buffers1 + cached1 + used2free2 = buffers1 + cached1 + free1
Buffer cache, also known as bcache, is called a buffer high-speed buffer memory in Chinese. In addition, buffer cache is also known as block high latency based on its working principle.
When reading and writing files in Linux, it is used to cache disk blocks on the physical disk, thus accelerating access to data on the disk.
The buffer cache content corresponds to a block on the disk. The block size is usually 1 K and is continuous.
In Linux, the operating system automatically uses all idle memory as the buffer cache for more effective use of physical memory. WhenProgramWhen more memory is needed, the operating system automatically reduces the cache size.
Difference between buffer and Cache
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.
For more details, see difference between buffer and cache.
Shared Memory is mainly used to share data between different processes in UNIX environments. It is a method for inter-process communication. Generally, applications do not apply for shared memory, I have not verified the effect of shared memory on the above equation. If you are interested, see: What is shared memory?
Differences between cache and buffer:
Cache: high-speed cache is a memory with a small capacity but high speed located between the CPU and the main memory. Because the CPU speed is much higher than the master memory, it takes a certain period of time for the CPU to directly access data from the memory. The cache stores part of the data that has just been used by the CPU or is used cyclically, when the CPU uses this part of data again, it can be called directly from the cache, which reduces the CPU wait time and improves the system efficiency. Cache is divided into Level 1 cache (L1 cache) and level 2 cache (L2 cache). L1 cache is integrated into the CPU, and L2 cache is usually soldered to the motherboard in the early stages, it is also integrated into the CPU. The common capacity is kb or kb.
L2 cache.
Buffer: a buffer used to transmit data between devices with Different Storage speeds or between devices with different priorities. Through the buffer zone, mutual waits between processes can be reduced, so that when reading data from a slow device, the operation process of the fast device will not interrupt.
Buffer and cache in free: (both occupy memory ):
Buffer: the memory used as the buffer cache. It is the read/write buffer of Block devices.
Cache: Memory Used as the page cache, the cache of the file system
If the cache value is large, it indicates that the number of cached files is large. If files frequently accessed can be cached, the disk read Io is very small.