1. Caching mechanism
in order to improve the performance of the file system, the kernel uses a portion of physical memory to allocate buffers for caching system operations and data files, and when the kernel receives read-write requests, the kernel first goes to the buffer zone to find out if the requested data is available, directly returns it, and if not, operates the disk directly through the driver.
Caching mechanism advantages: Reduce the number of system calls, reduce CPU context switching and disk access frequency.
CPU Context switching:The CPU gives each process a certain amount of service time, and when the time slice is exhausted, the kernel reclaims the processor from the running process, saves the current running state of the process, and then loads the next task, which is called context switching. is essentially a process switch that terminates the running process and the process to be run.
2. Check the buffer and memory usage
[root@localhost ~]# free-m
Total used free shared buffers cached
mem:7866 7725 141 6897
-/+ buffers/cache:752 7113
swap:16382 16350
can see a total memory of 8G, has used 7725M, the remaining 141M, a lot of people are so see, this does not serve as the actual usage. Because of the caching mechanism, how to calculate the specific?
free Memory =free (141) +buffers (+cached) (6897)
used Memory =total (7866)-Free memory
This calculates that the free memory is 7112M, has used memory 754M, this is the real usage rate, also can refer to-/+ Buffers/cache This line of information is also the correct memory usage rate.
3, the visible buffer is divided into buffers and cached, what difference do they have?
the kernel allocates the buffer size to ensure that the system can normally use physical memory and data volume reading and writing. Buffers is used to cache metadata and pages, which can be understood as a system cache, for example, vi to open a file. Cached is used to cache files, which can be understood as data block caching, for example, DD If=/dev/zero of=/tmp/test count=1 bs=1g Test Write a file, it will be cached in the buffer, the next time you execute this test command, The write speed will be noticeably faster.
4, casually say swap what to do with it?
swap means the swap partition, which is usually what we call virtual memory, which is a partition that is partitioned from the hard disk. When the physical memory is not enough, the kernel releases some long-unused programs in the cache (Buffers/cache) and then temporarily puts them into swap, which means that swap is used if the physical memory and buffer memory are not enough.
5, how to release buffer memory it?
5.1 Directly change kernel operating parameters
#释放pagecache
Echo 1 >/proc/sys/vm/drop_caches
#释放dentries和inodes
Echo 2 >/proc/sys/vm/drop_caches
#释放pagecache, dentries and Inodes
Echo 3 >/proc/sys/vm/drop_caches
5.2 You can also reset kernel run parameters using Sysctl
sysctl-w vm.drop_caches=3
Note: Both of these methods are temporary in effect, the permanent effect needs to be added in the sysctl.conf file, generally written script manual cleanup, it is recommended not to clean.
Linux system caching mechanism