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
1
2
3
4
5
$ free -wth
              total        used        free      shared     buffers       cache   available
Mem:           125G        6.5G         87G        1.3G        3.5M         30G        116G
Swap:            0B          0B          0B
Total:         125G        6.5G         87G

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
for_each_zone(zone)
   wmark_low += zone->watermark[WMARK_LOW];

/*
* Estimate the amount of memory available for userspace allocations,
* without causing swapping.
*
* Free memory cannot be taken below the low watermark, before the
* system starts swapping.
*/
available = i.freeram - wmark_low;

/*
* Not all the page cache can be freed, otherwise the system will
* start swapping. Assume at least half of the page cache, or the
* low watermark worth of cache, needs to stay.
*/
pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
pagecache -= min(pagecache / 2, wmark_low);
available += pagecache;

/*
* Part of the reclaimable swap consists of items that are in use,
* and cannot be freed. Cap this estimate at the low watermark.
*/
available += global_page_state(NR_SLAB_RECLAIMABLE) -
        min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low);

if (available < 0)
   available = 0;

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.