In linux, free is usually used to view the memory status.
What is the difference between cached and buffers in the free command of linux?
Free
Mem: physical memory statistics
-/+ Buffers/cached: indicates the cache statistics of physical memory.
Swap: usage of Swap partitions on the hard disk
The total physical memory of the system is 8098060 8 Gb, but the actual available memory of the system is not the 6054972Kb marked as free in the first line. It only indicates the unallocated memory.
We use names such as total1, used1, free1, used2, and free2 to represent the values of the preceding statistics. 1 and 2 represent the data of the first and second rows respectively.
Total: total physical memory.
Used: indicates the total quantity allocated to the cache (including buffers and cache), but some of the caches are not actually used.
Free: unallocated memory.
Shared: shared memory, which is generally not used by the system and will not be discussed here.
Buffers: Number of buffers allocated by the system but not used.
Cached: Number of cache allocated by the system but not used. The difference between buffer and cache is described later.
Note:
Total = used + free
Used = buffers + cached (maybe add shared also)
Difference between buffer and cache
A buffer is something that has yet to be "written" to disk. A cache is something that has been "read" from the disk and stored for later use.
Shared memory is mainly used to share data between different processes in UNIX environments. It is a method for inter-process communication. Generally, applications do not apply for Shared memory, I have not verified the effect of shared memory on the above equation.
Differences between cache and buffer:
Cache: high-speed Cache is a memory with a small capacity but high speed located between the CPU and the main memory. Because the CPU speed is much higher than the master memory, it takes a certain period of time for the CPU to directly access data from the memory. The Cache stores part of the data that has just been used by the CPU or is used cyclically, when the CPU uses this part of data again, it can be called directly from the Cache, which reduces the CPU wait time and improves the system efficiency. Cache is divided into Level 1 Cache (L1 Cache) and level 2 Cache (L2 Cache). L1 Cache is integrated into the CPU, and L2 Cache is usually soldered to the motherboard in the early stages, it is also integrated into the CPU. The common capacity is 256KB or 512KB L2 Cache.
Buffer: a Buffer used to transmit data between devices with Different Storage speeds or between devices with different priorities. Through the buffer zone, mutual waits between processes can be reduced, so that when reading data from a slow device, the operation process of the fast device will not interrupt.
Buffer and cache in Free: (both occupy memory ):
Buffer: the memory used as the buffer cache. It is the read/write buffer of Block devices.
Cache: Memory Used as the page cache, the cache of the file system
If the cache value is large, the number of files in the cache is large. If files frequently accessed can be cached, the disk read IO bi will be very small.
Memory release mechanism
Official descriptions of drop_caches are as follows:
Writing to this file causes the kernel to drop clean caches, dentries and inodes from memory, causing that memory to becomefree.
To free pagecache, use echo 1>/proc/sys/vm/drop_caches;
To free dentries and inodes, use echo 2>/proc/sys/vm/drop_caches;
To free pagecache, dentries and inodes, use echo 3>/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects are not freeable, the user shocould run sync first.
/Proc is a virtual file system. We can use its read/write operations as a means to communicate with the kernel object. in other words, you can modify the file in/proc to adjust the current kernel behavior. then we can release the memory by adjusting/proc/sys/vm/drop_caches. the procedure is as follows:
# Cat/proc/sys/vm/drop_caches
0
First, the value of/proc/sys/vm/drop_caches. The default value is 0.
Manually execute the sync command
Description: The sync command runs the sync subroutine. If you must stop the system, run the sync command to ensure the integrity of the file system. The sync command writes all unwritten system buffers to the disk, including modified I-nodes, delayed block I/O, and read/write ing files.
# Sync
# Echo 3>/proc/sys/vm/drop_caches
# Cat/proc/sys/vm/drop_caches
3
Set/proc/sys/vm/drop_caches to 3
Run "free" again to check that the memory has been released.