In Linux, we generally do not need to release the memory because the system has managed the memory well. But there are exceptions in everything. Sometimes the memory will be occupied by the cache, which will affect the system's performance by using SWAP space. In this case, you need to release the memory (clear the cache.
The Caching mechanism in Linux is quite advanced. It will be used for dentry (for VFS, to accelerate the conversion from file path to inode) and Buffer Cache (for disk block read/write) and Page Cache (for file inode read/write. However, after a large number of file operations, the cache will basically use up memory resources. But in fact, we have completed file operations, and this part of the cache is no longer available. At this time, can we only watch the cache to occupy the memory space?
Therefore, it is necessary to manually release the memory in Linux, that is, to release the cache.
To release the cache, we need to first understand the key configuration file/proc/sys/vm/drop_caches. This file records the cache release parameters. The default value is 0, that is, do not release the cache. The value can be 0 ~ Any number between 3 represents different meanings:
0-do not release
1-release page Cache
2-release dentries and inodes
3-release all caches
After knowing the parameters, we can use the following commands to perform operations as needed.
First, we need to use the sync command to write all the unwritten system buffers to the disk, including modified I-nodes, delayed block I/O, and read/write ing 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 the caches, enter the following command:
# Echo 3>/proc/sys/vm/drop_caches
This command takes effect immediately after it is entered. You can check that the available memory is significantly increased.
To query the parameters of the current cache release, enter the following command:
# Cat/proc/sys/vm/drop_caches