How does buffer and cache make you so difficult to understand?

Source: Internet
Author: User

  For a will embark on the "system operation" or the more tall still work "system tuning", if this does not have a good relationship with these two buddies, the pit is not only memory, but also the pit is that you take the tune of the money but dry at any time the live. Because as a system operator, monitoring and optimizing IO performance is the most likely skill you will survive, why? Because you not only save money for the boss, but also improve the efficiency of the machine. Although the money in the boss pocket, but you gradually implanted in his deep mind, one day you are more important than money! Okay, gossip, what's the next two buddies?

Before I myself also to exactly what buffer and cache is what, what is different, when with buffer, when with the cache doubts, can not say do not know, just people asked to speak up, I know, but talk about their differences, it really can't say. Helpless check a lot of information, either too low or vague, academic re-authoritative again insightful, so that the reader can not understand also useless, this is with a good program, you how to run in the kernel space, but if you do not provide a user space interface, with the person can not through a call interface to know you, that means you really cow, Cattle to use the people do not know what you are. The end of the service is user-oriented, you say some machine language every day, you really happy? So I talk about my own understanding of the buffer and the cache, hoping to help some people, of course, there are inappropriate places also ask teachers to give points. Your point is the best evaluation for me, thank you!

In both literal and semantic terms, buffer is called Buffering and the cache name is cached. We know that the different kinds of hardware exist in the production process differences, so when the two hardware need to interact, there will certainly be a speed difference, and only the interaction between the two sides can be completed to deal with other other transactions. If there are now two interfaces that require interaction between device A and the B,A device to interact with the interface rate of 1000M/S,B devices used to interact with 500m/s, then they will have the following two situations when they visit each other: (In a)

A. A from B to take a 1000M file results need 2s, it would have required 1s to complete the work, but also need to wait for the 1S,B device to find out the remaining 500M, which waits for B to take out the remaining 500M in the free time (1s) other business can not do

Two. A to B a 1000M file results also need 2s, originally need also on 1s can complete work, but because B,1s can only take 500M, the remaining 500M also have to wait for the next 1sB to take, this wait for the next 1s time also can not do other affairs.

What is the way to get a in ' take ' or ' give ' B when you can accomplish the goal task and not waste the 1s idle wait time to deal with other matters? We know that this result is mainly because B can't keep up with a rhythm, but even then a must wait for B to finish the transaction before doing anything else (single-core CPU), unless you have three heads. There are small partners may ask, can you add a layer between A and B, such as AB, so that AB can keep up with a frequency will also take care of B's feelings, yes we can do this design to run-in interface rate differences, you can imagine, in the region AB provides two interface one is a interface and the other is a B interface , the rate of a interface is at least the rate of B, and then we connect AB A and a, AB B and B, AB is like a bridge link A and B, and tell A and B through he can be forwarded to each other, the file can be temporarily stored, the final topology is probably as follows:

                

Now let's look at both of these cases:

For the first case A to B: when a from B to take a 1000M file, he told the demand AB, then AB through B and b for file transfer, because of the rate of B itself, transmission of the first AB and no eggs, for a not only wasted time and wasted affection, AB This guy soon felt the dissatisfaction of a, so in the second transmission, AB secretly cached a copy of the same file, and as long as from the B to take things, AB will cache a copied down in their own base, if the next time a or other C to take B's things, AB directly to a or c a genuine fake, and then pass it through a interface to a or C, because a is relatively close to the rate of a interface rate, so a feel good for him to save time, the final and AB a became a good base friends, plainly speaking at this time AB provides is a kind of caching capabilities, namely, the cache, absolute smuggling! Because c takes the result of a execution. So in this mode of work, how to get things is the latest and we need to consider, is generally clear cache. For example, the CPU reads the memory data, the hard disk generally provides a memory as the cache to increase the system read performance

    

For the second case a gives B: When a is sent to B a 1000M file, because a knows through the A interface AB can be forwarded to B, and through a interface to transfer the file through the B to wait for a shorter time, so 1000M through a interface to the AB, From a view he thought that the 1000M file has been given to B, but for AB is not immediately handed over to B, but the first cache, unless B to perform the sync command, even if b immediately, but because the interface rate of B is at least greater than the B interface rate, so there is no vulnerability time, But the end result is a save time can do other business, white is to shirk responsibility, haha and AB at this time provides is a kind of buffering ability, that is, buffer, it exists for the purpose of being fast to slow output things. For example, the memory data is written to the disk, and the data in the CPU register is written to memory.

Looking at the example above, let's take a look at the computer field, Cpu,memory,disk is based on an example given by this model when processing disk IO reads and writes. Let's start with a picture: (I come from the other, I think, see N documents than the picture)

                

Page cache: File system level cache, the content read from the disk is stored here, so the program reads the disk content will be very fast, such as grep and find and other commands to find content and files, the first time is much slower, the execution is many times faster, almost instantaneous. But as mentioned above, if the update of the file does not care, there is no need to clear the cache, otherwise if you want to implement synchronization, the memory space must be in the cache clean

Buffer cache: buffering of block devices such as disks, this part of memory is to be written to disk. This situation needs to be noted that the data in the memory buffer is not immediately written to disk, but the system is idle or buffer reached a certain size unified write to the disk, so the power loss is volatile, in order to prevent data loss so we'd better shut down or more than a few sync commands, The data on the buffer is immediately written to disk.

Next, how to view Buffer/cache on Linux, how to Flush...cache?

[Email protected] ~]# free-m

Total used free shared buffers Cached
mem:727 359 367 0 36 171
-/+ buffers/cache:152 575
swap:2047 0 2047

The first part of the MEM line:

Total: Memory Totals

Used: Number of memory already in use

Free: The number of idle memory

GKFX: Available Shared memory

Buffers: Number of memory buffers

Cached: Memory Cache Count

Part II: (-/+ Buffers/cache)

Used: Removal of memory used as buffers and cache memory

Free: Memory used as buffers and cache plus mem partially idle memory

Part III: (SWAP)

Use a portion of the disk as memory "memory"

Now let's do some data statistics.

Total=mem_used+mem_free

Mem_used=mem_buffers+mem_cached+ (-/+ buffers/cache) _used

Mem_free= (-/+ buffers/cache) _free-mem_buffers-mem_cached

With the above equation, we can know:

1.buffers and cache are also part of the address space divided by Ram

The address space of the 2.buffers and cache can also be used as part of the free memory, which means that we can release some memory to other processes by passing the parameters to the kernel

3. Since Buffers/cache is a dynamic memory address space, the use of space and free space is absolutely used, absolute free space, calculate the relative space of Buffers/cache and relative free space four concepts.

How do I release the cache? We know that Linux is an important idea of all documents, such as all kinds of input: keyboard, mouse, network card, monitor, printer, u disk, console port, in the country of Linux can create a file for it to access the device's entrance, and the parameters of the kernel is also mapped to a file, However, the kernel parameter of this kind of file is very special, there are two pseudo-file systems on Linux:/proc,/sys.

/proc: Output interface for kernel state and statistics: also provides a configuration interface,/proc/sys/some files can accept the user to specify a new value to implement a kernel function or feature configuration: Remember not to use a text editor to open, There are three ways we can modify this:

1). sysctl [Options] [Var=[value]]

var format x.x.x ... The default root directory of the command is/proc/sys

Sysctl-a: View kernel parameters in all root directories (can be sent to grep to retrieve required kernel parameters)

Sysctl var: View the values of the specified kernel parameters (if you are familiar with this directory you can refer directly to the kernel option you want to change)

Sysctl-w Var=value: Sets the value of the kernel parameter var equals value

2). Cat/proc/sys/path/var_file

echo "Value" >/proc/sys/path/var_file (via redirection)

Attention! The settings of the above two methods only take effect immediately, the kernel reload will be invalidated, if you want to make permanent effect need to modify its configuration file

3) configuration file:/etc/sysctl.conf,/etc/sysctl.d/*.conf (* is on behalf of all) this can be vim, such as we open through vim and let it have packet forwarding function, we can modify the following parameter # Con Trols IP packet Forwardin Net.ipv4.ip_forward = 1 Modified we can use Sysctl-p [/etc/sysctl.conf] to let its kernel reread the configuration file so that it modifies the Valu E takes effect immediately. Now that we know the kernel parameters are modified, we clean up the following buffers/cache[[email protected] ~]# echo 1 >/proc/sys/vm/drop_caches

[Email protected] ~]# free-m
Total used free shared buffers Cached
mem:727 177 549 0 0 27
-/+ buffers/cache:150 577
swap:2047 0 2047

We see that the command executes after buffers is 0,cached and only 27M.

Summarize:

Both 1.buffer and cache are designed to address the rate differences between two devices for mutual visits, making disk IO read-write performance or CPU more efficient, reducing the time between interprocess communication waits

2.buffer: Buffer-for storage speed is not synchronized between devices or different priority devices transfer data, through buffer can reduce the time between interprocess communication, when the storage speed of the device and storage slow devices to communicate, the storage of fast devices first cache data to buffer , wait until the system unifies the data on the buffer to write to the slow device. It is common to have memory data written to disk, you can check the buffers

3.cache: Buffer-used for reading speed is more stringent, but because the device because of the speed difference between the storage device, and can not immediately obtain data, the cache will be to speed up the cached part of the data. The most common is the data communication between the CPU and memory, because the CPU speed is much higher than the speed of main memory, the CPU reads the data from memory to wait for a long time, and the cache holds the data that the CPU has just used or the part of the data that is recycled, then the cache reads the data faster, It reduces CPU wait time and improves the performance of the system.

Problem:

1.buffers and cache are required?

2. How do I clear buffers and cache?

3. What does it mean when the buffers value increases over time? The cache has been growing for some time.

 

    

How does buffer and cache make you so difficult to understand?

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.