caching mechanisms in Linux
In the Linux system, in order to improve the file system performance, the kernel uses a portion of physical memory to allocate buffers, for caching system operations and data files, when the kernel receives read and write requests, the kernel first go to the buffer to find whether the requested data, there is a direct return, if not through the driver directly manipulate the disk.
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.
differentiate between buffers and cache
Buffers
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 the file, it can be understood as a block cache, 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 significantly faster.
How to release the cache
Cleanup Pagecache (page cache)
1 >/proc/sys/vm/drop_caches or # sysctl-w vm.drop_caches=1
Clean up dentries (directory cache) and Inodes
2 >/proc/sys/vm/drop_caches or # sysctl-w vm.drop_caches=2
Clean up Pagecache, dentries and Inodes
3 >/proc/sys/vm/drop_caches or # sysctl-w vm.drop_caches=3
The above three methods are temporary release of the cache, to permanently release the cache, you need to configure in the/etc/sysctl.conf file: VM.DROP_CACHES=1/2/3, and then sysctl-p effective!
You can also use the Sync command to clean up the file system cache, and also clean up Zombie (zombie) objects and the memory they occupy
(It's a good idea to do this before you clean up the cache)
# Sync
Caching mechanism of Linux learning