The role of buffer and cache in linux operating system

Buffer  and  cache  (they both take up memory).

Buffer :  The number of buffers  allocated by the system but not in use .

  

Buffer  buffer :   It is the read and write buffer of the block device. The buffer  is an I/O  cache designed for the speed of data exchange between memory and hard disk (or other  I/O devices).


 analyze:

1. Usually when writing a very large file, the file will be divided into small  blocks --> always write to memory -- > and then write to disk ,   this file is very large, but it will be divided into a A small block , little by little every time --> write to memory --> write to disk ,   this is slower.

2. In  this case, the memory will accumulate a large block once --> then write to the disk, so that there will be no delay in the first case. This is the buffer.

 


 Cache cache  : cache is a cache, used for buffering between cpu and memory. The main reason is cpu and memory. Because cpu is fast, memory cannot keep up, and some values ​​are used many times, so they are put into cache . The main purpose is to use memory to cache data that may be accessed again.  Cache  is often used for I/O  requests. To improve system performance.

When reading from the hard disk:

For example,  to open a very large video file from hard disk --> read to memory --> display it. It takes some time to open the file for the first time (depending on the performance of the computer), and then the second time it opens it will be much smoother than the first time.

 For example, when Xiao Ming watched this 10G video file for the first time, he went to the hard disk --> memory --> display. After watching it, he shut down the computer and emptied the memory space. I want to review the plot again. At this time, read from the hard disk --> to the memory. At this time, the memory is not read from the hard disk again, but there is a cache before the shutdown, and the reading time may be smoother than before. This is the cache. Yes In order to improve the efficiency of file reading.

 

If  the value of cache is large , it means that there are many files in the cache . If frequently accessed files can be cached, then the read IO of the disk will be very small.

 


Now do an experiment:

Now buff / cache   is 100

 Use cat b.txt to read the file from disk to memory:

Buffer / cache  increased to 103

  Using memory, that is, using buffers and caches 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806925&siteId=291194637