When using linux operating systems, it is a common requirement to check memory usage, for example, to analyze system health or to determine if the machine is ready to deploy more new services, etc. This is a common requirement.
The solution to this problem is also very simple. Most cloud vendors provide consoles that display monitoring information for various machine metrics, and memory usage monitoring is definitely essential. For those who know a little bit about linux, you can also use the free command to view more detailed information. When executing the free command, I wonder if you have the same confusion as I do.
- What do all the columns in the printout mean? How do these columns relate to each other?
- Which indicator should I look at for memory availability, free or available?
1. Basic use of the free command
Basic use: free [options]
Common parameters:
-h
human-readable format printing-w
print cache & buffer separately-t
show total for RAM + swap
2. Meaning of each column of the free command
The free command prints information from /proc/meminfo
(/proc is not a directory on the physical disk, but a virtual directory used to provide kernel information), and parses the contents of the file to show the usage of physical memory and Swap memory.
metrics | meaning | /proc/meminfo data source |
---|---|---|
total | Total physical memory available, but not kernel & OS memory usage | MemTotal |
free | The amount of memory space that is not used | MemFree |
buffers | the amount of space used for caching by the kernel | Buffers |
cache | the amount of free memory used for process page cache and slabs (slab is used as OS cache) | Cached & SReclaimable |
used | calculated by other parameters | total - free - buffers - cache |
shared | memory space used as tmpfs | Shmem |
avaliable | predicted memory space available for starting a new application | MemAvailable |
3. Should I focus on the free column or avaliable for available memory?
To determine how much memory space is available for starting a new process, you need to pay attention to the avaliable value. The avaliable value is usually larger than the free value, because it adds some of the space occupied by cache & buffer. However, it is smaller than free + cache + buffers.
The existence of cache is due to the need to improve performance in OS operation. In accordance with the locality principle, process memory pages that may be used in the future are cached, reducing the probability of a page-out interruption the next time that data is needed. Because every time a page-out interrupt occurs, the process needs to be put out, perform disk I/O to read the data, and then put the process into the ready state, which is a time-consuming operation. Not doing the necessary cache will result in frequent disk I/O, memory jitter, and ultimately low CPU usage.
The avaliable statistics do not include all cache & buffer, because it is reasonable for the system to do some caching for good performance. From source, the system calculates the available memory by subtracting min(pagecache / 2, wmark _low)
, which is the minimum value between 1/2 the cache size and the minimum amount of free memory to be saved, which will not be counted as free memory. So that’s why the true avaliable value is larger than free and smaller than free + cache + buffers.
|
|
4. Conclusion
free
is a common command when operating a linux system, and there is a certain threshold for understanding the intrinsic meaning of its various metrics. Memory management in operating systems is complex, and the essential problem is how to use the limited memory space to maximize the number of processes and reduce the rate of out-of-page interruptions.