It is described in Documentation/sysctl/vm.txt as follows:
drop_caches:Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
As this is a non-destructive operation and dirty objects are not freeable, the user should run `sync' first.
2. The difference between cache and buffer
Cache: High-speed cache is a small-capacity but high-speed memory located between the CPU and the main memory. Since the speed of the CPU is much higher than that of the main memory, it takes a certain period of time for the CPU to directly access data from the memory. A part of the data that the CPU has just used or recycled is stored in the Cache. When the CPU uses this part of the data again, it can be retrieved from the Cache. Invoke directly, thus reducing the waiting time of the CPU and improving the efficiency of the system. Cache is further divided into first-level Cache (L1 Cache) and second-level Cache (L2 Cache). L1 Cache is integrated inside the CPU. L2 Cache is usually welded on the motherboard in the early days, and now it is also integrated inside the CPU. The common capacity is 256KB. or 512KB L2 Cache.
Buffer: Buffer, an area used to store data transferred between devices with asynchronous speeds or devices with different priorities. Through the buffer, the mutual waiting between the processes can be reduced, so that when data is read from the slow device, the operation process of the fast device will not be interrupted.
Buffer and cache in Free: (they all take up memory):
buffer: the memory used as buffer cache, which is the read and write buffer of the block device
cache: memory as page cache, file system cache
If the value of cache is large, it means that there are many files in cache. If frequently accessed files can be cached, the disk read IO bi will be very small.
Linux/Centos/Ubuntu clears the system cache and releases the memory command specific operation steps
In order to improve disk access efficiency, Linux has made some careful designs. In addition to caching dentry (used in VFS to speed up the conversion of file path names to inodes), it also adopts two main Cache methods: Buffer Cache and Page Cache. The former is for reading and writing of disk blocks, and the latter is for reading and writing of file inodes. These Cache effectively shorten the time of I/O system calls (such as read, write, getdents). The calculation method available for Linux memory: available memory = free+buffers+cached, clear the cache in the following order:
free related options:
lpf@ubuntu:~$ free -h
free: invalid option -- 'h'
usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]
-b,-k,-m,-g show output in bytes, KB, MB, or GB
-l show detailed low and high memory statistics
-o use old format (no -/+buffers/cache line)
-t display total for RAM + swap
-s update every [delay] seconds
-c update [count] times
-V display version information and exit
1. Current memory usage: free -m
lpf@ubuntu:~$ free -m
total used free shared buffers cached
Mem: 495 481 14 0 137 217
-/+ buffers/cache: 126 368
Swap: 509 0 509
Free command explanation:
total total memory
used The amount of memory used
free free memory
shared The total amount of memory shared by multiple processes
buffers Buffer Cache and cached Page Cache disk cache size
-buffers/cache memory: used - buffers - cached
+buffers/cache memory: free + buffers + cached
2. sync (The sync command runs the sync subroutine. If the system must be stopped, run the sync command to ensure the integrity of the file system. The sync command writes all unwritten system buffers to disk, including the modified i- node, delayed block I/O, and read-write mapped files, be sure to run this command before step 3)
3、root@ubuntu:/home/lpf# echo 3 > /proc/sys/vm/drop_caches
4. Check memory usage: free
root@ubuntu:/home/lpf# free -m
total used free shared buffers cached
Mem: 495 151 344 0 1 46
-/+ buffers/cache: 102 392
Swap: 509 0 509
5、echo 0 > /proc/sys/vm/drop_caches
This is a relatively common method to release the cache, but it is recommended not to use it frequently. If the swap is not used a lot, you can do nothing to ensure the stable operation of the system. Once the swap starts to be used, consider analyzing it. Program or increase physical memory. Under normal circumstances, when the application runs stably on the system, the free value will also remain at a stable value, although it may seem relatively small. When there are problems such as insufficient memory, the application cannot obtain available memory, OOM errors, etc., it is still more necessary to analyze the application reasons, such as insufficient memory due to too many users, application memory overflow, etc. Otherwise, clear the buffer and force Freeing up the size of free may only temporarily shield the problem.