The free command makes it easy to understand how much memory is used, how much is free, and how much is used for caching. The actual free command pulls memory information from Linux /proc/meminfo. Let's take a look at the memory information you can see with the free command and the values of meminfo associated with it.
Linux always tries to use free memory space as buffers and caches. By storing data in memory, it improves performance by reducing access to slow disks as much as possible.
Buffer is the size of the buffer cache caching metadata about the device block in memory. It stores the information needed to read data from the block device in memory.
Cache is the amount of memory being used as a page cache and slab.
In fact, if you create a file of about 1M in size and read it, you can see the cache size grow.
Cache size before reading files
Read a file 1M in size
Cache size increased by 1M
More information about the amount of recently unused memory in cache and buffer can be found in /proc/meminfo.
The sizes shown above, Inactive(anon), Inactive(file), and SRelaimable, are the sizes that can be moved to the swap area and allocated as new memory because they have been referenced for a long time.
These values are referenced by the free command to show how much of the data cached in memory can be pushed out to the swap area and freed up for new allocations, with available in the free command. available is only an estimate calculated by the system, and should not be relied upon too heavily.
Swap is a portion of disk that is set aside to be used as memory when memory space is low.
When the kernel runs out of memory, it attempts to free up things that are allocated as buffers and caches that are not used often and allocate them to memory. It also moves data in memory that has not been used recently to SWAP space (SWAP-OUT).
And when the process reads the data moved to SWAP, it brings the data back into memory to read it.(SWAP-IN) This increases latency because it brings data that was moved from disk back into memory, which eventually leads to performance degradation.
You should constantly monitor your SWAPs to determine when you are using a certain amount of SWAP due to a temporary increase in memory usage, and when your SWAPs are growing due to an ongoing memory shortage, and consider when to increase memory.
Programs that load-balance or distribute workloads also consult /proc/meminfo for information about how much memory can be allocated by emptying the caching area. You can also manually use commands to empty caching. However, proper use of caching can improve system performance, and emptying caching too frequently can lead to performance degradation. Therefore, you should consider setting the kernel parameters for caching appropriately based on the characteristics of your system.
https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/21/lec.html
https://cs.stackexchange.com/questions/45159/can-someone-explain-this-diagram-about-slab-allocation