The caching mechanism of the Linux system is quite advanced. It targets dentry (for VFS, accelerating the conversion of file path names to inodes), Buffer Cache (for reading and writing disk blocks) and Page Cache (for reading and writing file inodes). ) to perform caching operations. However, after a large number of file operations are performed, the cache will basically use up the memory resources. But in fact, our file operation has been completed, and this part of the cache is no longer used. At this time, can we just watch the cache occupy the memory space?
So, we still need to manually release the memory under Linux, which is actually the operation of releasing the cache.
To achieve the purpose of releasing the cache, we first need to understand the key configuration file /proc/sys/vm/drop_caches. This file records the parameters of cache release. The default value is 0, which means the cache is not released. Its value can be any number between 0 and 3, representing different meanings:
0 – Not released
1 – Release page cache
2 – Release dentries and inodes
3 – Free all caches
After knowing the parameters, we can use the following instructions to operate according to our needs.
First we need to use the sync command to write all unwritten system buffers to disk, including modified i-nodes, delayed block I/O and read-write mapping files. Otherwise, unsaved files may be lost during the cache release process.
#sync
Next, we need to write the required parameters into the /proc/sys/vm/drop_caches file. For example, if we need to release all caches, enter the following command:
#echo 3 > /proc/sys/vm/drop_caches
This command will take effect immediately after being entered, and you can check that the available memory has obviously increased.
To query the parameters released by the current cache, you can enter the following command:
#cat /proc/sys/vm/drop_caches
The above is an introduction to how Linux uses commands to release the cache. If you feel that your computer is getting more and more stuck, it may be that your memory is occupied a lot. You might as well clean up the computer garbage.
The above is the detailed content of Linux uses instructions to release cache methods. For more information, please follow other related articles on the PHP Chinese website!