free Command shows the usage of system memory , Including physical memory 、 Swap memory (swap) And kernel buffer memory .

If you add -h Options , The output will be much more friendly :

Sometimes we need to continuously observe the state of memory , You can use -s Option and specify the number of seconds between :

$ free -h -s 3

The order above is every 3 Second output of memory usage , Until you press ctrl + c.

because free The command itself is relatively simple , So the focus of this article will be on how to pass free Command to understand the current memory usage of the system .

Introduction to output

Let's explain the output first :
Mem That's ok ( The second line ) It's memory usage .
Swap That's ok ( The third line ) It's the use of swap space .
total Column shows the total available physical memory and swap space of the system .
used Column shows the physical memory and swap space that has been used .
free Column shows how much physical memory and swap space is available .
shared Column shows the amount of physical memory being shared .
buff/cache Column display is buffer and cache Size of physical memory used .
available Column shows the amount of physical memory that can also be used by the application .

I think only after understanding some basic concepts , The above output can help us understand the memory status of the system .

buff/cache

Let's start with a question : buffer and cache There should be two types of memory , however free Why do orders put them together ? To answer this question, we need to do some preparatory work . Let's make it clear first buffer And cache The meaning of .

buffer In operating system, refers to buffer cache, The Chinese translation is generally " buffer ". To understand buffers , There are two other concepts that have to be made clear :" A sector " and " block ". A sector is the smallest addressing unit of a device , Also called " Hard sector " or " Device block ". A block is the smallest addressing unit of a file system in an operating system , Also called " File block " or "I/O block ". Each block contains one or more sectors , But it can't be larger than one page , So a page can hold one or more blocks in memory . When a block is called into memory , It's going to be stored in a buffer . Each buffer corresponds to a block , It is equivalent to the representation of disk block in memory ( The figure below is from the Internet ):

Be careful ,buffer cache There's only the concept of blocks, not files , It just moves the blocks on the disk directly to the memory, and does not care what format of files are stored in the blocks .

cache In operating system, refers to page cache, The Chinese translation is generally " Page cache ". Page cache is the disk cache implemented by kernel . It's mainly used to reduce the demand for disks I/O operation . Specifically speaking , By caching the data on the disk into physical memory , Turning disk access into physical memory access . The page cache caches memory pages . The pages in the cache come from normal files 、 Block device file ( This means buffer cache ah ) And read and write memory mapped files .
Page cache is the cache of ordinary files. We can understand it in this way : When the kernel wants to read a file ( such as /etc/hosts) when , It will first check whether the data of this file is already in the page cache . If in , Give up access to the disk , Read directly from memory . This behavior is called cache hit . If the data is not in the cache , Just miss the cache , At this point, the kernel will schedule the blocks I/O Operation reads data from disk . Then the kernel puts the read data into the page cache . The target of this cache is the files that the file system can recognize ( such as /etc/hosts).
Page cache caching of block device files is what we described earlier buffer cahce. Because independent disk blocks are also stored in the page cache through buffers ( The buffer is ultimately hosted by the page cache ).

We should have made it clear here : Whether it's buffer or page cache , They are all implemented in the same way . Buffer is just a special page cache in concept .
So why free The command is not directly called cache Instead of writing buff/cache? This is because the implementation of buffers and page caching is not inherently uniform . stay linux kernel 2.4 Only in China can they be unified . Earlier kernels had two separate disk caches : Page cache and buffer cache . The former caches pages , The latter cache buffer . When you know these stories , The name of the column in the output may no longer matter .

free And available

stay free In the output of the command , There is one free Column , And there's another one available Column . What's the difference between the two ?
free Is the amount of physical memory that has not really been used . as for available It's more interesting , It's the amount of memory available from an application's point of view .Linux In order to improve the performance of disk operation , Will consume part of memory to cache disk data , That's what we introduced buffer and cache. So for the kernel ,buffer and cache All belong to the memory that has been used . When an application needs memory , If not enough free Memory can be used , The kernel will start from buffer and cache Reclaim memory in order to satisfy the application's request . So from an application perspective ,available  = free + buffer + cache. Please note that , It's just an ideal way to calculate , The data in practice often have large errors .

Swap space (swap space)

swap space It's an area on a disk , It could be a zone , It can also be a file . So the specific implementation can be swap Partitions can also be swap file . When the physical memory of the system is tight ,Linux It will save the data that is not frequently accessed in memory to swap On , In this way, the system has more physical memory for each process , And when the system needs to access swap When storing content on , then swap The data on is loaded into memory , This is often referred to as swap out and swap in . Swap space can alleviate the lack of memory to some extent , But it needs to read and write disk data , So the performance is not very high .

Today's machines are generally not short of memory , If the system still uses swap Will it drag down the performance of the system ? Theoretically, yes , But it's not very likely . And the kernel provides a program called swappiness Parameters of , It is used to configure the data that is not commonly used in memory to swap The degree of urgency . The value range of this parameter is 0~100,0 Tell the kernel not to move memory data to swap in , That is to say, only when you have to , and 100 Tell the kernel that whenever possible , Try to move the infrequently accessed data in memory to swap in . stay ubuntu In the system ,swappiness The default value of is 60. If we feel we have enough memory , Can be in /etc/sysctl.conf Set in file swappiness:

vm.swappiness=10

If the system is out of memory , You need to set the size of the swap space according to the size of the physical memory . Specific strategies there is a wealth of information on the Internet , Here I will not repeat .

/proc/meminfo file

Actually free The information in the command comes from /proc/meminfo file ./proc/meminfo The file contains more original information , It just doesn't look intuitive :

$ cat /proc/meminfo

 

Interested students can view this file directly .

summary

free Command is a simple and complex command . It's simple because there are few arguments to this command , The output is clear . It's complicated because behind it are obscure concepts in the operating system , If you don't understand these concepts , Even look at it. free The output of the command is also get Less valuable information .