In the Linux operating system, we generally do not need to release the memory, because the system has managed the memory well. But there are exceptions to everything. For example: 1 when the memory is occupied by the cache, causing the system to use swap space and ultimately affect system performance; 2 in some environments where monitoring software is deployed to detect memory usage, the alarm occurs when memory usage reaches a certain limit (for example: 80%). Therefore, in these scenarios, it is necessary to perform an operation to free the memory (cleanup cache).
The Linux system's caching mechanism is quite advanced, and he caches operations for Dentry (for VFS, for speeding file path names to inode conversions), buffer cache (for disk block reads and writes), and page cache (for file Inode reads and writes). However, after a large number of file operations, the cache will basically run out of memory resources. But in fact our file operation has been completed, this part of the cache has not been used. At this time, we can only helplessly watch the cache to occupy the memory space?
Therefore, we still need to manually release the memory under Linux operation, in fact, is to release the cache operation.
To achieve the goal of freeing caching, we first need to understand the key configuration file/proc/sys/vm/drop_caches. The cache-freed parameter is recorded in this file, with the default value of 0, which is not releasing the cache. His value can be any number between 0~3, representing different meanings:
0– not released
The last page cache release
2– release Dentries and Inodes
3– Releases all Caches
Once we know the parameters, we can use the following instructions to do the work according to our needs.
First we need to use the Sync directive to write all the unused system buffers to disk, including modified I-node, deferred block I/O, and read-write mapping files. Otherwise, unsaved files may be lost during the release of the cache.
#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 instruction will take effect immediately after input, you can query the current available memory significantly more.
To query the current cache-freed parameters, you can enter the following directive:
#cat/proc/sys/vm/drop_caches
Let's take a look at the actual test:
Look at memory usage first
[Root@node1 ~]# Free-m
Total used free shared buffers Cached
mem:8004 6557 1446 0 163 5630
-/+ buffers/cache:763 7240
swap:1983 0 1983
The data in the memory is temporarily written to the hard drive.
[Root@node1 ~]# Sync
modifying/proc/sys/vm/drop_caches files
[Root@node1 ~]# echo 3 >/proc/sys/vm/drop_caches
[Root@node1 ~]# Cat/proc/sys/vm/drop_caches
Then look at the memory, finally released.
[Root@node1 ~]# Free-m
Total used free shared buffers Cached
mem:8004 631 7372 0 0 60
-/+ buffers/cache:570 7433
swap:1983 0 1983