Differences between memory buffer and cache in Linux

Source: Internet
Author: User

Differences between memory buffer and cache in Linux operating system & free parameter description

Let's start with the Free command. Www.2cto.com
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 free1 shared1 buffers1 cached1
Mem: 255268 238332 16936 0 85540 126384
Used2 free2
-/+ Buffers/cache: 26408 228860

Swap: 265000 0 265000
Total1 = used1 + free1
Total1 = used2 + free2
Used1 = buffers1 + cached1 + used2

Free2 = buffers1 + cached1 + free1
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.
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.
[B] What is the difference between cache and buffer? [/B]
[B] Cache: high-speed Cache is a memory with a small but high speed located between the CPU and the main memory. [/B] 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 256KB or 512KB L2 Cache.
[B] Buffer: a Buffer zone used to transmit data between devices with Different Storage speeds or between devices with different priorities. [/B] through the buffer, mutual waits between processes can be reduced, so that when reading data from a slow device, the operation process of the fast device is uninterrupted.
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, the number of files in the cache is large. If files frequently accessed can be cached, the disk read IO will be very small.

The buffer is closer to the storage device or directly the disk buffer;
The cache is the mem buffer;
~~~~~~~~~~~~~~~~~~~~~~~~~~ Because of the LINUX kernel mechanism, you generally do not need to release the used cache. The cached content can increase the file and read/write speed.
Let's talk about how to view memory with the free command first.

[Root @ yuyii proc] # free
Total used free shared buffers cached
Mem: 515588 295452 220136 0 2060 64040
-/+ Buffers/cache: 229352 286236
Swap: 682720 112 682608
The first line describes the memory usage of the system from a global perspective:
Total -- total physical memory
Used -- memory used. Generally, this value is relatively large because it includes the memory used by the cache + application.
Free -- completely unused memory
Shared-application shared memory
Buffers -- cache, mainly used for directories and inode values (this value can be seen in large ls Directories)
Cached-Cache for opened files
Note:
Total = used + free
Used = buffers + cached (maybe add shared also)
The second line describes the memory usage of the application:
The previous value indicates-buffers/cache-the memory size used by the application, and used minus the cache Value
The last value indicates + buffers/cache-all memory sizes available for the application, free and cache values
Note:
-Buffers/cache = used-buffers-cached
+ Buffers/cache = free + buffers + cached
The third line indicates the usage of swap:
Used -- used
Free -- unused
Release cache:
To free pagecache:
Echo 1>/proc/sys/vm/drop_caches
To free dentries and inodes:
Echo 2>/proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
Echo 3>/proc/sys/vm/drop_caches
Note: It is best to sync before release to prevent data loss.



 

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.