Operation series (17) -- linux memory cleanup/release command

Under the Linux system, we generally do not need to release the memory, because the system has managed the memory very well. But there are exceptions to everything. Sometimes the memory will be occupied by the cache, causing the system to use the SWAP space to affect the performance. At this time, it is necessary to perform the operation of releasing the memory (cleaning the cache).

The caching mechanism of the Linux system is quite advanced. It will target dentry (for VFS, to speed up the conversion of file pathnames to inodes), Buffer Cache (read and write for disk blocks) and Page Cache (read and write for file inodes) perform a cache operation. But after a lot of file operations, the cache will basically run out of 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 take up the memory space?

Therefore, it is still necessary for us 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, that is, the cache is not released. Its value can be any number between 0 and 3, representing different meanings:

0 - do not free
1 - free page cache
2 - free dentries and inodes
3 - free all caches

After knowing the parameters, we can use the following commands 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 and write mapped files. Otherwise, unsaved files may be lost during the process of releasing the cache.

  1. Synchronization instruction
sync

1. Memory usage before cleaning

free -m

2. Start cleaning

echo 1 > /proc/sys/vm/drop_caches

3. Memory usage after cleaning

free -m

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325562640&siteId=291194637